- 论坛徽章:
- 0
|
在oracle 9i上使用rank分析函数,在主查询上使用order by col,当col是分析函数所在的字段时,查询只能返回一行,如果是order by col desc则正常,不知道这个算不算是bug?
SQL> select * from all_sales;
YEAR MONTH PRD_TYPE_ID EMP_ID AMOUNT ID
---------- ---------- ----------- ---------- ---------- ----------
2003 10 1 21 21754.19 10
2003 1 2 21 10014.84 11
2003 2 2 21 15844.65 12
2003 3 3 21 20937.83 13
2003 4 3 21 25357.45 14
2003 5 4 21 17714.56 15
2003 6 4 21 16564.64 16
2003 7 5 21 12684.84 17
2003 8 5 21 12434.82 18
2003 9 3 21 19154.57 19
2003 10 3 21 21554.19 20
已选择11行。
SQL> select
2 prd_type_id,sum(amount),
3 RANK() OVER (ORDER BY SUM(amount) DESC) AS rk
4 from all_sales
5 group by prd_type_id;
PRD_TYPE_ID SUM(AMOUNT) RK
----------- ----------- ----------
3 87004.04 1
4 34279.2 2
2 25859.49 3
5 25119.66 4
1 21754.19 5
SQL> select
2 prd_type_id,sum(amount),
3 RANK() OVER (ORDER BY SUM(amount) DESC) AS rk
4 from all_sales
5 group by prd_type_id
6 order by rk;
PRD_TYPE_ID SUM(AMOUNT) RK
----------- ----------- ----------
5 107770.95 1
SQL> select
2 prd_type_id,sum(amount),
3 RANK() OVER (ORDER BY SUM(amount) DESC) AS rk
4 from all_sales
5 group by prd_type_id
6 order by rk desc;
PRD_TYPE_ID SUM(AMOUNT) RK
----------- ----------- ----------
1 21754.19 5
2 25119.66 4
5 25859.49 3
3 34279.2 2
4 87004.04 1
SQL> |
|