简体   繁体   English

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

[英]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? 如何使用SQL(IBM DB2)/ Informatica或两者结合使用?

Note : Can't use stored procs. 注意:不能使用存储的proc。

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. OLAP函数对将一行与下一行进行比较确实很有帮助。 An UNION is necessary to create additional rows. 必须创建一个UNION才能创建其他行。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否有在 sql 表中的两条记录之间添加新记录的语法? - Is there any syntax for adding new record between two records in sql table? 如何使用SQL从带有逗号分隔值字段的单个记录创建新记录(在Oracle数据库中) - How to create new records from a single record with a comma delimited value field using SQL (in Oracle Database) 如何使用row_number创建一个在两条记录之间的差异上返回true或false的新字段 - How to create a new field that returns true or false on the difference between two records using row_number 使用祖父母记录配对两个记录? SQL - Pairing two records using Grandparent record? SQL 如何检测SQ​​L中两个日期时间之间的重叠? - How to detect overlapping between two datetime in SQL? 仅使用SQL查找和标记分组记录之间的重叠日期 - Finding and flagging overlapping dates between grouped records using only SQL SQL - 根据 2 个日期之间的天数从 1 条记录创建多条记录 - SQL - Create multiple records from 1 record based on days between 2 dates SQL:需要为每个单个记录创建两个唯一记录 - SQL: Need to create two unique records for each single record 如何从 sql 查询创建 informatica 映射 - How to create informatica mapping from sql query 使用 BigQuery 计算列中重复记录之间的不同记录 SQL - Count Distinct Records between a repeating record in column using BigQuery SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM