oracle 开窗函数 over,sql(oracle)数据处理实用总结开窗函数(over partition)使用

1.开窗函数over partition

7423ea2f954a

开窗函数使用

开窗函数使用于取出多列分组,取一列分组下另一组前几名,先利用开窗函数对其分组排名,开窗函数排名函数较多使用row_number(),还有rank()等,生成排名列之后将结果集筛选其排名前几<=2或者你想要的前几名。

上述例子sql:select test_name,test_id,n_rank,test_salary_sum from ( select t_alias.test_name,t_alias.test_id, sum(t_alias.test_salary) test_salary_sum, row_number() over(partition by t_alias.test_name order by sum(t_alias.test_salary) desc) n_rank from (select 1 test_id, 'name_1' test_name, 100 test_salary from dual union all select 2 test_id, 'name_1' test_name, 200 test_salary from dual union all select 2 test_id, 'name_1' test_name, 300 test_salary from dual union all select 3 test_id, 'name_1' test_name, 300 test_salary from dual union all select 4 test_id, 'name_1' test_name, 400 test_salary from dual union all select 1 test_id, 'name_2' test_name, 200 test_salary from dual union all select 2 test_id, 'name_2' test_name, 200 test_salary from dual union all select 2 test_id, 'name_2' test_name, 200 test_salary from dual union all select 3 test_id, 'name_2' test_name, 400 test_salary from dual union all select 4 test_id, 'name_2' test_name, 400 test_salary from dual) t_alias group by t_alias.test_name,t_alias.test_id) where n_rank<=2 order by test_name,n_rank;

上述执行结果:

7423ea2f954a

开窗函数实例执行结果

获得每个test_name字段下test_id的test_salary的前2名。

开窗函数实例:

7423ea2f954a

开船函数实例

另有开窗函数替代写法,可以了解:

7423ea2f954a

开窗函数替代写法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值