2017-02-09 48 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 

不過,我將不勝感激寫這個查詢,以儘量減少資源消耗的有效途徑。