2017-08-13 72 views
0

我有如下所示,並且仍有成千上萬行這樣查找不具有連續的日期範圍在DB2行

MBR  MBR_SPAN  EFF_DT END_DT   
1 B 1/1/2011 12/31/2011 
1 C 1/1/2012 12/31/2012 
1 A 2/1/2013 12/31/2013 
2 D 1/1/2010 12/31/2010 
2 X 1/1/2011 12/31/ 2011 

我需要找到的行中每個成員的數據它是不連續的與以前的日期範圍。在這種情況下,它是MBR 1和MBR_SPAN A

我沒有一個連續的列進行排序並確定哪個列應該有連續的日期範圍。它必須通過對比前一行來決定(可能通過排序eff_dt)

而且它不會產生任何臨時表,因爲我不能夠獲得創建DB2表來完成。

誰能幫助?

回答

1

這裏有一個方法:

select * 
from (select t.*, 
      lag(end_dt) over (partition by mbr order by eff_dt) as prev_end_dt 
     from t 
    ) t 
where end_dt <> prev_end_dt + 1 day and prev_end_dte is not null; 
+0

非常感謝回覆。它按預期工作 –

0

變化:假設你的表被稱爲「指明MyDate」,你想一個單行結果從上面的樣本數據集:
select * from (select t.*, lag(end_dt) over (partition by mbr order by eff_dt) as prev_end_dt from mydate as t ) x where x.eff_dt <> x.prev_end_dt + 1 day and x.prev_end_dt is not null

0

其他方法:

with tmp as 
(
    select f1.*, rownumber() over (partition by f1.mbr order by f1.eff_dt, f1.END_DT) as rang 
    from yourtablename f1 
) 
select f1.* from tmp f1 
inner join tmp f2 on f1.mbr=f2.mbr and f1.rang=f2.rang-1 and f1.eff_dt + 1 day <> f2.eff_dt