简体   繁体   中英

How to create a new record for the overlapping period between two records using Informatica/SQL

For below source data a new record needs to be created for the overlapping period and its the amount should be the sum of the overlapping record's amount. The start date and end date of the existing records also need to be changed so that they do not overlap.

Source :

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

Expected Output :

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

How can I do this using either SQL(IBM DB2)/Informatica or a combination of both?

Note : Can't use stored procs.

The place to start is by splitting the data so there is only one column with the amount. I think this produces what you want:

select id, dte as StartDate,
       lead(dte) over (partition by id, dte) - 1 day as NextDate,
       sum(sum(amount)) over (partition by id order by dte) as amount
from ((select id, startdate as dte, amount
       from t
      ) union all
      (select id, enddate + 1 day, - amount
       from t
      )
     ) t
group by id, dte;

OLAP functions can really be helpful comparing one row with the next one. An UNION is necessary to create additional rows. The following example handles each type of row individually.

-- 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM