根据每个员工从最高到最低的绩效点对每个员工进行排名

时间:2018-02-21 10:26:32

标签: mysql sql

创建两个表emp_merits(meritid,empid,date,meritpoints),emp1(empid,empname)每个员工每个月都会根据他们的表现给出优点。因此,同一个员工可以在表中有多个具有不同优点的条目。 列出特定员工在特定日期之间收到的所有优点(这里将输入empid) 根据每个员工从最高到最低的绩效点对每个员工进行排名

到目前为止,我已尝试过此查询

select   empid , sum (meritpoints) as totalmerits , (DENSE_RANK()OVER (PARTITION BY empid ORDER BY meritpoints desc)) AS rank from emp_merit 
  group by empid,meritpoints 
  order by empid ,totalmerits desc

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

SELECT @rownum := @rownum + 1 AS rank, a.*  
FROM (
SELECT empid, sum(meritpoints) AS totalmerits
FROM emp_merits 
GROUP BY empid
ORDER BY totalmerits) a, (SELECT @rownum := 0) r ;

您可能需要WHERE子句中的特定日期。

答案 1 :(得分:0)

您可以使用变量实现dense_rank()

select empid, totalmerits,
       (@rn := if(@m = totalmerits, @rn,
                  if(@m := totalmerits, @rn + 1, @rn + 1)
                 )
       ) as rank
from (select empid, sum(meritpoints) as totalmerits
      from emp_merit 
      group by empid
      order by totalmerits desc
     ) e cross join
     (select @m := -1, @rn := 0) params;