另类的ORACLE分析函数over(partition by...)

本文通过一个具体的SQL实例,详细解析了如何在Oracle分析函数over(partitionby......)中结合CASEROW_NUMBER()实现灵活的数据分组排序。包括不使用CASEROW_NUMBER()的效果对比,以及对特定ID值进行CASEROW_NUMBER()的具体应用,展示了其在数据处理中的独特优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

oracle 分析函数使用的另类:

over (partition by ......) 分析函数中使用case when ;

例:

SQL> with pctest as (
  2  SELECT 1 id ,'a1' name from dual
  3  union
  4  SELECT 1 id ,'a2' name from dual
  5  union
  6  SELECT 1 id ,'a3' name from dual
  7  union
  8  SELECT 2 id ,'b1' name from dual
  9  union
 10  SELECT 2 id ,'b1' name from dual
 11  union
 12  SELECT 2 id ,'b2' name from dual
 13  union
 14  SELECT 3 id ,'b1' name from dual
 15  union
 16  SELECT 4 id ,'c1' name from dual
 17  union
 18  SELECT 4 id ,'c2' name from dual
 19  )
 20  select * from pctest
 21  ;

        ID NAME
---------- ----
         1 a1
         1 a2
         1 a3
         2 b1
         2 b2
         3 b1
         4 c1
         4 c2

8 rows selected

不使用case when 效果:

SQL> with pctest as (
  2  SELECT 1 id ,'a1' name from dual
  3  union
  4  SELECT 1 id ,'a2' name from dual
  5  union
  6  SELECT 1 id ,'a3' name from dual
  7  union
  8  SELECT 2 id ,'b1' name from dual
  9  union
 10  SELECT 2 id ,'b1' name from dual
 11  union
 12  SELECT 2 id ,'b2' name from dual
 13  union
 14  SELECT 3 id ,'b1' name from dual
 15  union
 16  SELECT 4 id ,'c1' name from dual
 17  union
 18  SELECT 4 id ,'c2' name from dual
 19  )
 20  select id,row_number()
 21  over (partition by id order by id ) as rn  from pctest
 22  ;

        ID         RN
---------- ----------
         1          1
         1          2
         1          3
         2          1
         2          2
         3          1
         4          1
         4          2

8 rows selected



使用case when 效果(对数字例id 进行case when ):

这里在case when中使用的是等于号,你也可以在case when中指定其他任何不同的条件表达式,可以达到你想要的特定的分组效果

SQL> with pctest as (
  2  SELECT 1 id ,'a1' name from dual
  3  union
  4  SELECT 1 id ,'a2' name from dual
  5  union
  6  SELECT 1 id ,'a3' name from dual
  7  union
  8  SELECT 2 id ,'b1' name from dual
  9  union
 10  SELECT 2 id ,'b1' name from dual
 11  union
 12  SELECT 2 id ,'b2' name from dual
 13  union
 14  SELECT 3 id ,'b1' name from dual
 15  union
 16  SELECT 4 id ,'c1' name from dual
 17  union
 18  SELECT 4 id ,'c2' name from dual
 19  )
 20  select id,row_number()
 21  over (partition by case when id=1 then 1  when id=2 then 2 end order by id ) as rn from pctest
 22  ;

结果:

        ID         RN

---------- ---------- 

         1          1    ---- id=1的一个分组
         1          2
         1          3
         2          1  ---- id=2的一个分组
         2          2
         3          1  ----id=其他的一个分组
         4          2
         4          3

8 rows selected

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值