标签:demo data 技术 where font weight ima day inf
有以下数据表:事件标识表未这个股票进入(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
标签:demo data 技术 where font weight ima day inf
原文地址:https://www.cnblogs.com/champaign/p/13255925.html