如何在Oracle中根据开始和结束日期对记录进行重复数据消除

kjthegm6  于 2023-05-22  发布在  Oracle
关注(0)|答案(1)|浏览(363)

我尝试合并一个表,以便每个记录都表示给定ID-Case键的一个时间段,并且每个时间段都不会与具有相同键的其他记录重叠。例如,我有这个表:

我希望结果表看起来像这样:

***********************这应被视为连续记录。

我一直在尝试使用LEAD和LAG OVER PARTITION函数,但无法弄清楚。任何帮助都很感激。

8tntrjer

8tntrjer1#

您可以使用MERGE语句和MATCH_RECOGNIZE来查找重叠:

MERGE INTO table_name t
USING (
  SELECT rowid AS rid,
         rn,
         MAX(enddate) OVER (PARTITION BY mn) AS enddate
  FROM   table_name
  MATCH_RECOGNIZE (
    PARTITION BY id, code
    ORDER BY startdate, enddate
    MEASURES
      COUNT(*) AS rn,
      MATCH_NUMBER() AS mn
    ALL ROWS PER MATCH
    PATTERN ( overlap* last_row )
    DEFINE overlap AS NEXT(startdate) <= MAX(enddate) + 1
  )
) m
ON (t.ROWID = m.rid)
WHEN MATCHED THEN
  UPDATE
  SET   enddate = m.enddate
  DELETE WHERE rn > 1

其中,对于样本数据:

CREATE TABLE table_name (id, code, startdate, enddate) AS
  SELECT 1, 'X', DATE '2023-01-01', DATE '2023-01-30' FROM DUAL UNION ALL
  SELECT 1, 'X', DATE '2023-02-01', DATE '2023-02-28' FROM DUAL UNION ALL
  SELECT 1, 'X', DATE '2023-02-10', DATE '2023-02-28' FROM DUAL UNION ALL
  SELECT 1, 'Y', DATE '2023-01-01', DATE '2023-01-31' FROM DUAL UNION ALL
  SELECT 1, 'Y', DATE '2023-02-01', DATE '2023-02-28' FROM DUAL UNION ALL
  SELECT 1, 'Y', DATE '2023-03-01', DATE '2023-03-31' FROM DUAL;

MERGE之后,该表包含:
| ID|代码|开始日期|结束|
| --------------|--------------|--------------|--------------|
| 1| X| 2019 -01- 21 00:00:00| 2023-03-31 00:00:00|
| 1| X| 2019 -02- 21 00:00:00| 2019 -02-28 00:00:00|
| 1| Y轴|2019 -01- 21 00:00:00| 2023-03-31 00:00:00|
fiddle

相关问题