在postgres中排名总数

时间:2017-02-09 09:04:09

标签: sql postgresql

我正在编写一个sql脚本,其中我希望获得每个销售人员的总约会数,然后获得他在其他销售人员中的排名。例如,销售人员x有5个任命,他对10个销售人员中的4个进行评级。

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

2 个答案:

答案 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

答案 1 :(得分: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

但是,我希望能够有效地编写此查询以最大限度地减少资源消耗。