简体   繁体   中英

Oracle SQL data migration based on start and end date

Working on migrating old system data to new system.

I need to group the data based on id and name. We need to start date as min date and end date as max date. If any id and name combination contains falls under the same period. We can avoid duplicate and choose lowest to highest date.

Legacy System

在此处输入图像描述

New System Expectation

在此处输入图像描述

ID - 139247 contains duplicate rows based on name.

Added data in - https://dbfiddle.uk/?rdbms=oracle_18&fiddle=8d6877847c5e052adf703430b5c7f083

Please let me know if more details needed. Thanks in advance.

This is a type of gaps-and-islands problem. Because you want any overlaps, I would go for a cumulative max of the previous enddate to determine where the islands being:

select id, name, min(startdate) as startdate,
       (case when count(enddate) = count(*) then max(enddate)
        end) as enddate
from (select t.*,
             sum(case when prev_enddate >= startdate then 0 else 1 end) over (partition by id, name) as grp
      from (select t.*,
                   max(enddate) over (partition by id, name order by startdate range between unbounded preceding and interval '1' day preceding) as prev_enddate
            from t
           ) t
      ) t
group by id, name, grp
order by name, startdate;

Here is a db<>fiddle.

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