2017-02-09 10 views
-1

私は営業担当者あたりの総アポイントメント数を取得し、残りの営業担当者のランクを取得するSQLスクリプトを作成しています。たとえば、営業担当者xは5人の予定で、10人の営業担当者のうち4人を評価します。postgresの合計からのランク

**expected results**: 
Salesperson x 5 4/10 
Salesperson D 6 5/10 
Salesperson s 8 7/10 
+0

あなたが試したコードを投稿できますか? –

答えて

0

使用rank()

with sales as 
(
select Salesperson, count(appointment) appointments 
from SalesTable 
group by Salesperson 
) 
select sales.*, rank() over (order by appointments desc) as salesrank 
from sales 
0

こんにちはご回答いただきありがとうございます。この方法で試してみました。

select id,sales_person,"Appointment/Day",rank_for_the_day,"Appointment/Week",rank_for_the_week,"Appointment/Month", 
rank_for_the_month,"Appointment/year",rank_for_the_year 
from(
select supplied_id,salesperson,sum(case when appointment_date::date=current_date then 1 else 0 end)"Appointment/Day", 
    rank() over (order by sum(case when appointment_date::date=current_date then 1 else 0 end) desc)||'/'|| 
    (select sum(case when appointment_date::date=current_date then 1 else 0 end) from match where date_part('year', appointment_date)=2017 
    and appointment_date is not null and date_part('day',appointment_date)=date_part('day',current_date)) rank_for_the_day, 

    sum(case when appointment_date::date between current_date-7 and current_date then 1 else 0 end)"Appointment/Week", 
    rank() over (order by sum(case when appointment_date::date between current_date-7 and current_date then 1 else 0 end) desc)||'/'|| 
      (select sum(case when appointment_date::date between current_date-7 and current_date then 1 else 0 end) 
      from match m where date_part('year', appointment_date)=2017 and appointment_date is not null 
      and date_part('week',appointment_date)=date_part('week',current_date)) rank_for_the_week, 

    sum(case when date_part('month',appointment_date)=date_part('month',current_date) then 1 else 0 end)"Appointment/Month", 
     rank() over (order by sum(case when date_part('month',appointment_date)=date_part('month',current_date) then 1 else 0 end) desc)||'/'|| 
      (select sum(case when date_part('month',appointment_date)=date_part('month',current_date) then 1 else 0 end) 
      from match m where date_part('year', appointment_date)=2017 and appointment_date is not null 
      and date_part('month',appointment_date)=date_part('month',current_date)) rank_for_the_month, 

     sum(case when date_part('year',appointment_date)=date_part('year',current_date) then 1 else 0 end)"Appointment/year", 
    rank() over (order by sum(case when date_part('year',appointment_date)=date_part('year',current_date) then 1 else 0 end) desc)||'/'|| 
    (select sum(case when date_part('year',appointment_date)=date_part('year',current_date) then 1 else 0 end) 
      from match m where date_part('year', appointment_date)=2017 and appointment_date is not null 
      and date_part('year',appointment_date)=date_part('year',current_date)) rank_for_the_year 
from salespersontable 
where date_part('year', appointment_date)=2017 and appointment_date is not null 
group by id,salesperson 
)x order by 6 desc 

しかし、リソース消費を最小限に抑えるためにこのクエリを書くと効率的です。

関連する問題