从我的数据库中我想得到员工的姓氏,收入高于他的员工人数,然后按降序排列员工的工资;收入最高的员工为零。我是这样做的:
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
结果显示如下:
到目前为止一切顺利。除此之外,博格不应该在那里。因为没有人比他赚得更多。现在发生这种情况是因为使用了LEFT OUTER JOIN。我尝试使用INNER JOIN,但它给出了语法错误。所以问题是如何让INNER JOIN与之合作呢?
答案 0 :(得分:1)
如果您只想消除最高值:
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