oracle 根据日期更新重复的行

aemubtdh  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(89)

我有下面的SQL表,其中有重复的行,不同的sdates将来会有相同的edate

user   sdate        edate   
-------------------------------
 A     01.05.2023   01.01.2099
 A     13.07.2023   01.01.2099
 B     01.04.2023   01.01.2099
 B     25.08.2023   01.01.2099
 C     15.05.2023   01.01.2099
 C     15.07.2023   01.01.2099

我想更新旧行的edates,如下所示

user    sdate           edate   
----------------------------------
 A      01.05.2023      13.07.2023
 A      13.07.2023      01.01.2099
 B      01.04.2023      25.08.2023
 B      25.08.2023      01.01.2099
 C      15.05.2023      15.07.2023
 C      15.07.2023      01.01.2099

有什么方法可以处理1000行的表吗?

9nvpjoqh

9nvpjoqh1#

您可以使用ROW_NUMBER()和LEAD()分析函数:

WITH
    tbl AS
        (   Select 'A' "USR", To_Date('01.05.2023', 'dd.mm.yyyy') "SDATE", To_Date('01.01.2099', 'dd.mm.yyyy') "EDATE" From Dual Union All
            Select 'A' "USR", To_Date('13.07.2023', 'dd.mm.yyyy') "SDATE", To_Date('01.01.2099', 'dd.mm.yyyy') "EDATE" From Dual Union All
            Select 'B' "USR", To_Date('01.04.2023', 'dd.mm.yyyy') "SDATE", To_Date('01.01.2099', 'dd.mm.yyyy') "EDATE" From Dual Union All
            Select 'B' "USR", To_Date('25.08.2023', 'dd.mm.yyyy') "SDATE", To_Date('01.01.2099', 'dd.mm.yyyy') "EDATE" From Dual Union All
            Select 'C' "USR", To_Date('15.05.2023', 'dd.mm.yyyy') "SDATE", To_Date('01.01.2099', 'dd.mm.yyyy') "EDATE" From Dual Union All
            Select 'C' "USR", To_Date('15.07.2023', 'dd.mm.yyyy') "SDATE", To_Date('01.01.2099', 'dd.mm.yyyy') "EDATE" From Dual 
        )
Select      USR, SDATE, EDATE, 
          Case  When ROW_NUMBER() OVER(Partition By USR Order By SDATE) = 1
                Then Lead(SDATE) OVER(Partition By USR Order By SDATE)
          Else  EDATE
          End "NEW_EDATE"
From        tbl 
--  R e s u l t :
--  USR SDATE     EDATE     NEW_EDATE
--  --- --------- --------- ---------
--  A   01-MAY-23 01-JAN-99 13-JUL-23
--  A   13-JUL-23 01-JAN-99 01-JAN-99
--  B   01-APR-23 01-JAN-99 25-AUG-23
--  B   25-AUG-23 01-JAN-99 01-JAN-99
--  C   15-MAY-23 01-JAN-99 15-JUL-23
--  C   15-JUL-23 01-JAN-99 01-JAN-99

可以合并此结果以更新您的数据:

Merge Into tbl t
Using   (   Select  USR, SDATE, EDATE, 
                    Case    When ROW_NUMBER() OVER(Partition By USR Order By SDATE) = 1
                            Then Lead(SDATE) OVER(Partition By USR Order By SDATE)
                    Else  EDATE
                    End "NEW_EDATE"
            From        tbl 
        ) x ON(x.USR = t.USR And x.SDATE = t.SDATE And x.EDATE = t.EDATE)
When MATCHED 
Then UPDATE SET t.EDATE = x.NEW_EDATE
Where t.EDATE != x.NEW_EDATE

相关问题