简体   繁体   English

COUNT与RANK mysql

[英]COUNT with RANK mysql

From my database I want to get the lastname of employee, the number of employees who earn higher than him, and then rank the employee's salary in descending order; 从我的数据库中我想得到员工的姓氏,收入高于他的员工人数,然后按降序排列员工的工资; with the highest earning employee as zero. 收入最高的员工为零。 Here is how I have done it: 我是这样做的:

SELECT 0+COUNT(b.salary) rank
, a.lname
, a.salary
, COUNT(*) AS employee_count
FROM employee a LEFT OUTER JOIN employee b
ON a.salary<b.salary
GROUP BY a.salary, a.lname
ORDER BY salary DESC

And the result is shown like this: 结果显示如下:

在此输入图像描述

So far so good. 到现在为止还挺好。 Except, Borg shouldn't be even there. 除此之外,博格不应该在那里。 Because no one is earning more than him. 因为没有人比他赚得更多。 Now this happened because of using LEFT OUTER JOIN. 现在发生这种情况是因为使用了LEFT OUTER JOIN。 I tried using INNER JOIN but it gives syntax error. 我尝试使用INNER JOIN,但它给出了语法错误。 So the question is how can I make INNER JOIN work with this? 所以问题是如何使INNER JOIN与此一起工作?

If you just want to eliminate the top value: 如果您只想消除最高值:

SELECT *
FROM (    
    SELECT 0+COUNT(b.salary) rank
        , a.lname
        , a.salary
        , COUNT(*) AS employee_count
    FROM employee a 
    LEFT OUTER JOIN employee b
        ON a.salary < b.salary
    GROUP BY a.salary, a.lname
    ORDER BY salary DESC
) T1
WHERE T1.rank > 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM