zoukankan      html  css  js  c++  java
  • ROWNUM()应用案例-实现一个拉链表

    有以下数据表:事件标识表未这个股票进入(1)或者移除(2)某个股票池。我们需要对数据进行去重,就是连续多次的进入,或者连续多次的移除,都只取第一次的日期。

    如图中,黄色的记录是不需要的。(这个表命名为demo)

    首先第一步

     select SCODE,POOLID,EFFECTIVE_DAY,ADJUSTMODE
             ,row_number() over (partition by SCODE,POOLID,ADJUSTMODE order by EFFECTIVE_DAY) as rn
             ,row_number() over (partition by SCODE,POOLID order by EFFECTIVE_DAY) as rn2
      from  demo t 

    得到如下结果(我将进入的标注了红色):

     观察数据的规律,然后我们用RN2-RN1。再按SCODE+POOLID+ADJUSTMODE分组中,按日期进行排序。

    SQL:

    select x.*,rn2-rn,row_number() over (partition by scode,poolid,adjustmode,rn2-rn order by rn) as rn3
    from (      
      select SCODE,POOLID,EFFECTIVE_DAY,ADJUSTMODE
             ,row_number() over (partition by SCODE,POOLID,ADJUSTMODE order by EFFECTIVE_DAY) as rn
             ,row_number() over (partition by SCODE,POOLID order by EFFECTIVE_DAY) as rn2
      from  demo t 
      ) x
    order by 1,2,3

    然后发现RN3=1的就是我们需要的记录.

    完整的代码如下:


    with demo as (
      select '00001' as SCODE,1 POOLID,date'2010-01-01' as EFFECTIVE_DAY,1 as ADJUSTMODE from dual
          union all
          select '00001' as SCODE,1 POOLID,date'2010-01-03' as EFFECTIVE_DAY,1 as ADJUSTMODE from dual
          union all
          select '00001' as SCODE,1 POOLID,date'2010-01-05' as EFFECTIVE_DAY,2 as ADJUSTMODE from dual
          union all
          select '00001' as SCODE,1 POOLID,date'2010-01-07' as EFFECTIVE_DAY,2 as ADJUSTMODE from dual
          union all
          select '00001' as SCODE,1 POOLID,date'2010-02-01' as EFFECTIVE_DAY,1 as ADJUSTMODE from dual
          union all
          select '00001' as SCODE,1 POOLID,date'2010-02-03' as EFFECTIVE_DAY,1 as ADJUSTMODE from dual
          union all
          select '00001' as SCODE,1 POOLID,date'2010-02-07' as EFFECTIVE_DAY,2 as ADJUSTMODE from dual
          union all
          select '00002' as SCODE,1 POOLID,date'2010-01-01' as EFFECTIVE_DAY,1 as ADJUSTMODE from dual
          union all
          select '00002' as SCODE,1 POOLID,date'2010-01-05' as EFFECTIVE_DAY,2 as ADJUSTMODE from dual
          union all
          select '00002' as SCODE,1 POOLID,date'2010-01-07' as EFFECTIVE_DAY,2 as ADJUSTMODE from dual
          union all
          select '00002' as SCODE,1 POOLID,date'2010-02-01' as EFFECTIVE_DAY,1 as ADJUSTMODE from dual
          union all
          select '00002' as SCODE,1 POOLID,date'2010-02-03' as EFFECTIVE_DAY,1 as ADJUSTMODE from dual
          union all
          select '00002' as SCODE,1 POOLID,date'2010-02-07' as EFFECTIVE_DAY,2 as ADJUSTMODE from dual
    )  
    ,data as (
    select x.*,rn2-rn,row_number() over (partition by scode,poolid,adjustmode,rn2-rn order by rn) as rn3
    from (      
      select SCODE,POOLID,EFFECTIVE_DAY,ADJUSTMODE
             ,row_number() over (partition by SCODE,POOLID,ADJUSTMODE order by EFFECTIVE_DAY) as rn
             ,row_number() over (partition by SCODE,POOLID order by EFFECTIVE_DAY) as rn2
      from  demo t
      ) x
    )
    select * from data where rn3=1
    order by 1,2,3
  • 相关阅读:
    c#/asp.net实现炫酷仿调色板/颜色选择器功能
    c#程序员机试题
    如何定义最佳 Cache-Control 策略
    HTTP 缓存之ETag 和Cache-Control的使用方法介绍
    ASP.NET MVC 提高运行速度的几种性能优化方法
    c#FTP操作类,包含上传,下载,删除,获取FTP文件列表文件夹等Hhelp类
    C#实现GridView导出Excel
    C#文件上传类,文件流,字节数组等
    c#XML操作类的方法总结
    c#实现list,dataset,DataTable转换成josn等各种转换方法总和
  • 原文地址:https://www.cnblogs.com/champaign/p/13255925.html
Copyright © 2011-2022 走看看