用rank对oracle分组排序

前些日子做了个数据分组后重新排序的报表。是用crystal脚本写的,先设置一个全局变量,让其自加,然后在每一个group head里面清零。代码很简单,我就不贴了。
过段日子,有同事求一个分组后重新排序的语句,他以为我的报表是用类似的sql做的。其实如果用oracle的话,rank函数也可以实现分组后重新排序。
RANK Function
The RANK function produces an ordered ranking of rows starting with a rank of one. Users specify an optional
PARTITION clause and a required ORDER BY clause. The PARTITION keyword is used to define where the rank
resets. The specific column which is ranked is determined by the ORDER BY clause. If no partition is specified,
ranking is performed over the entire result set. RANK will assign a rank of 1 to the smallest value unless descending
order is used. The following example ranks salesmen for each region based on their sales amount.
SELECT sales_person, sales_region, sales_amount,
RANK() OVER (PARTITION BY s_region ORDER BY s_amount DESC)
FROM Sales_table;

sales_person ,sales_region ,sales_amount, rank
Adams           East           100           1
Baker           East           99            2
Connors         East           89            3
Davis           East           75            4
Edwards         West           74            1
Fitzhugh        West           66            2
Gariabaldi      West           45            3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值