-1
我正在写一个SQL脚本,我想要得到每个销售人员的约会总数,然后也得到多少他从其他销售人员排名。例如销售员x有5个预约,他对10个推销员中的4个进行评分。排名在postgres总数
**expected results**:
Salesperson x 5 4/10
Salesperson D 6 5/10
Salesperson s 8 7/10
我正在写一个SQL脚本,我想要得到每个销售人员的约会总数,然后也得到多少他从其他销售人员排名。例如销售员x有5个预约,他对10个推销员中的4个进行评分。排名在postgres总数
**expected results**:
Salesperson x 5 4/10
Salesperson D 6 5/10
Salesperson s 8 7/10
使用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
您好感谢您的答复。我尝试了这种方式它的工作原理:
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
不过,我将不胜感激写这个查询,以尽量减少资源消耗的有效途径。
你可以发布你试过的代码吗? –