SELECT prd_type_id,SUM(amount), RANK() OVER (ORDER BY SUM(amount) DESC NULLS LAST) AS rank FROM all_sales WHERE year=2003 GROUP BY ROLLUP(prd_type_id) ORDER BY rank;
PRD_TYPE_ID SUM(AMOUNT) RANK
1972485.13 1 (注:RULLUP的总计排在了最前)
1 905081.84 2
3 478270.91 3
4 402751.16 4
2 186381.22 5
5 6
SELECT prd_type_id,emp_id,SUM(amount), RANK() OVER (ORDER BY SUM(amount) DESC NULLS LAST) AS rank FROM all_sales WHERE year=2003 GROUP BY CUBE(prd_type_id,emp_id) ORDER BY prd_type_id,emp_id;
PRD_TYPE_ID EMP_ID SUM(AMOUNT) RANK
----------- ---------- ----------- ----------
1 21 197916.96 12
1 22 214216.96 10
1 23 98896.96 19
1 24 207216.96 11
1 25 93416.96 21
1 26 93417.04 20
1 905081.84 2
2 21 20426.96 33
2 22 19826.96 34
2 23 19726.96 35
2 24 43866.96 27
PRD_TYPE_ID EMP_ID SUM(AMOUNT) RANK
----------- ---------- ----------- ----------
2 25 32266.96 31
2 26 50266.42 24
2 186381.22 14
3 21 140326.96 15
3 22 116826.96 16
3 23 112026.96 17
3 24 34829.96 29
3 25 29129.96 32
3 26 45130.11 26
3 478270.91 3
4 21 108326.96 18
PRD_TYPE_ID EMP_ID SUM(AMOUNT) RANK
----------- ---------- ----------- ----------
4 22 81426.96 23
4 23 92426.96 22
4 24 47456.96 25
4 25 33156.96 30
4 26 39956.36 28
4 402751.16 6
5 21 36
5 22 36
5 23 36
5 24 36
5 25 36
PRD_TYPE_ID EMP_ID SUM(AMOUNT) RANK
----------- ---------- ----------- ----------
5 26 36
5 36
21 466997.84 4
22 432297.84 5
23 323077.84 8
24 333370.84 7
25 187970.84 13
26 228769.93 9
1972485.13 1
已选择42行。
SQL> SELECT
2 prd_type_id,emp_id,SUM(amount),
3 RANK() OVER (ORDER BY SUM(amount) DESC NULLS LAST) AS rank