如何使用Informatica / SQL为两个记录之间的重叠期创建新记录

时间:2016-02-22 13:02:06

标签: sql db2 informatica informatica-powercenter

对于以下源数据,需要为重叠期创建新记录,其金额应为重叠记录金额的总和。还需要更改现有记录的开始日期和结束日期,以使它们不重叠。

来源:

ID  StartDate EndDate   Amount
1   1-Jan     31-Jul    100
1   1-Jun     31-Dec    100

预期产出:

ID  StartDate EndDate   Amount
1   1-Jan     31-May    100
1   1-Jun     31-Jul    200
1   1-Aug     31-Dec    100

如何使用SQL(IBM DB2)/ Informatica或两者的组合来实现此目的?

注意:不能使用存储过程。

2 个答案:

答案 0 :(得分:0)

开始的地方是分割数据,因此只有一列有数量。我认为这会产生你想要的东西:

glReadPixels()

答案 1 :(得分:0)

OLAP函数在将一行与下一行比较时非常有用。需要UNION来创建其他行。以下示例分别处理每种类型的行。

-- normal rows without overlapping
select id, startdate, enddate, amount
  from ( select id, startdate, enddate, amount ,
                lead(startdate) over (partition by id order by startdate) as nextstart
           from t )
 where nextstart > enddate

 union all

-- overlapping time ranges

-- first intervall 
select id, startdate, nextstart - 1 day as enddate, amount
  from ( select id, startdate, enddate, amount ,
                lead(startdate) over (partition by id order by startdate) as nextstart
           from t )
 where nextstart < enddate

union all

-- new middle interval
select id, nextstart as startdate,  enddate, amount + nextamount
  from ( select id, startdate, enddate, amount ,
                lead(startdate) over (partition by id order by startdate) as nextstart,
                lead(amount) over (partition by id order by startdate) as nextamount
          from t )
  where nextstart < enddate

union all

-- last interval
select id, prevend + 1 day as startdate,  enddate, amount 
  from ( select id, startdate, enddate, amount ,
                lag(enddate) over (partition by id order by startdate) as prevend
           from t )
 where startdate < prevend