| emp_no | salary | rank |
|---|---|---|
| 10005 | 94692 | 1 |
| 10009 | 94409 | 2 |
| 10010 | 94409 | 2 |
| 10001 | 88958 | 3 |
| 10007 | 88070 | 4 |
| 10004 | 74057 | 5 |
| 10002 | 72527 | 6 |
| 10003 | 43311 | 7 |
| 10006 | 43311 | 7 |
| 10011 | 25828 | 8 |
SELECT emp_no, salary FROM salaries WHERE to_date = ‘9999-01-01‘ GROUP BY emp_no;
|
1
2
3
4
5
6
7
8
9
10
11
|
+--------+--------+| emp_no | salary |+--------+--------+| 10001 | 88958 || 10002 | 72527 || 10003 | 43311 || 10004 | 74057 || 10005 | 94692 || 10006 | 43311 || 10007 | 88070 |+--------+--------+ |
SELECT result1.emp_no, result1.salary, COUNT(DISTINCT result2.salary) AS rank FROM ( SELECT emp_no, salary FROM salaries WHERE to_date = ‘9999-01-01‘ GROUP BY emp_no ) AS result1, ( SELECT emp_no, salary FROM salaries WHERE to_date = ‘9999-01-01‘ GROUP BY emp_no ) AS result2 WHERE result2.salary >= result1.salary GROUP BY result1.emp_no ORDER BY result1.salary DESC, result1.emp_no ASC;
SELECT s1.emp_no, s1.salary, COUNT(DISTINCT s2.salary) AS rank FROM salaries s1, salaries s2 WHERE s1.to_date = ‘9999-01-01‘ AND s2.to_date = ‘9999-01-01‘ AND s2.salary >= s1.salary GROUP BY s1.emp_no ORDER BY s1.salary DESC, s1.emp_no ASC;
原文:https://www.cnblogs.com/Transkai/p/13388416.html