用于修复数据完整性问题的Oracle SQL合并查询 作业ID ETL作业运行日期客户ID客户ID客户名客户姓生效日期从生效日期到最新版本实际客户ID 123 01.01.20 00:00:00 100 6842托尼·格雷格10.01.00:00:00 31.12.99 23:59:59 1 Y 123 15.01.20 00:00:00 148 6842托尼·格雷格10.01.00:00:00 31.12.99 23:59:59 1 Y 9011

用于修复数据完整性问题的Oracle SQL合并查询 作业ID ETL作业运行日期客户ID客户ID客户名客户姓生效日期从生效日期到最新版本实际客户ID 123 01.01.20 00:00:00 100 6842托尼·格雷格10.01.00:00:00 31.12.99 23:59:59 1 Y 123 15.01.20 00:00:00 148 6842托尼·格雷格10.01.00:00:00 31.12.99 23:59:59 1 Y 9011,oracle,Oracle,这是错误的,因为一些错误的历史数据更新,所以预期值应该是这样的 作业ID ETL作业运行日期客户ID客户ID客户名客户姓生效日期从生效日期到最新版本实际客户ID 123 01.01.20 00:00:00 100 6842托尼·格雷格01.01.00:00:00 15.01.20 00:00:00 1 N 123 15.01.20 00:00:00 148 6842托尼·格雷格15.01.20 00:00:00 31.12.99 23:59:59 2 Y 9011 首先,我们需要一些主键来清楚地

这是错误的,因为一些错误的历史数据更新,所以预期值应该是这样的

作业ID ETL作业运行日期客户ID客户ID客户名客户姓生效日期从生效日期到最新版本实际客户ID 123 01.01.20 00:00:00 100 6842托尼·格雷格01.01.00:00:00 15.01.20 00:00:00 1 N 123 15.01.20 00:00:00 148 6842托尼·格雷格15.01.20 00:00:00 31.12.99 23:59:59 2 Y 9011
首先,我们需要一些主键来清楚地定义行的顺序。我们需要比较上一行和下一行,因此顺序是基本的。如果我们可以使用job_id、etl_job_run_day、cust_sid,那么您可以使用以下查询查找有问题的行:

create table abc
(
job_id integer
,etl_job_run_day date
,cust_sid integer
,cust_id number(38,0)
,cust_first_name varchar2(100)
,cust_last_name varchar2(100)
,effective_dt_from date
,effective_dt_to date
,version integer
,latest_fl varchar2(1)
,actual_cust_id integer
);

insert into abc values (123,01.01.2020,100,6842,'Tony','Greig',10.01.2020 00:00:00,31.12.2199 23:59:59,1,'Y','');
insert into abc values (123,01.01.2020,123,6842,'Tony','Greig',10.01.2020 00:00:00,31.12.2199 23:59:59,1,'Y',9011);
请测试它,如果确定,您可以在合并中使用上述查询作为源表:


这为我提供了更正样本数据所需的值。再次,请测试它。如果可能的话,复制一份。如果一行中有两个以上的位置,并且始终存在相同的版本,则需要额外的合并,但在这种情况下,应增加以下所有版本?您可以使用“按顺序计数”来完成此操作。。。在下一个类似的合并中。

Hallo,确切的问题是什么?我需要提出一个合并语句,以解决数据完整性问题,记住要点,由于我有很多记录的数据不一致,我必须提出一个合并语句来解决这个问题我需要提出一个合并语句来解决数据完整性问题,需要记住的要点,因为我有很多记录的不一致数据,所以我必须提出一个合并语句来解决这个问题,请将示例缩减为2-3列,在其中显示要归档的数据更改。对于merge语句,有两个问题。首先获取添加的数据。第二,正确书写带有on和update部分的语句。我们应该在哪一部分提供帮助?我已经尝试使用上述查询,但它不起作用。这是一个用于识别数据完整性问题的查询SELECT*from SELECT*-cust\u sid,版本,生效日期,生效日期,cust\u id from SELECT a.*,LEADeffective\u dt\u from,从下一个日期起生效的1份实际客户订单超额付款,领先版本,从abc a的下一个版本起生效的1份实际客户订单超额付款,其中生效日期至!=下一个日期或版本=下一个版本;-我走100行
select * 
  from (
    select job_id, etl_job_run_day, cust_sid, effective_dt_from edf, effective_dt_to edt, version, latest_fl, 
           lag(latest_fl)  over (partition by job_id order by etl_job_run_day, cust_sid) prev_fl,
           lead(latest_fl) over (partition by job_id order by etl_job_run_day, cust_sid) next_fl,
           lag(effective_dt_to)  over (partition by job_id order by etl_job_run_day, cust_sid) prev_edt,
           lead(effective_dt_to) over (partition by job_id order by etl_job_run_day, cust_sid) next_edt,
           lead(etl_job_run_day) over (partition by job_id order by etl_job_run_day, cust_sid) next_run
      from abc
      where latest_fl = 'Y' )
  where edt in (prev_edt, next_edt)
merge into abc a
using (
    select * 
      from (
        select job_id, etl_job_run_day, cust_sid, effective_dt_from edf, effective_dt_to edt, version, latest_fl, 
               lag(latest_fl)  over (partition by job_id order by etl_job_run_day, cust_sid) prev_fl,
               lead(latest_fl) over (partition by job_id order by etl_job_run_day, cust_sid) next_fl,
               lag(effective_dt_to)  over (partition by job_id order by etl_job_run_day, cust_sid) prev_edt,
               lead(effective_dt_to) over (partition by job_id order by etl_job_run_day, cust_sid) next_edt,
               lead(etl_job_run_day) over (partition by job_id order by etl_job_run_day, cust_sid) next_run
          from abc
          where latest_fl = 'Y' )
      where edt in (prev_edt, next_edt)) s
on (a.job_id = s.job_id and a.etl_job_run_day = s.etl_job_run_day and a.cust_sid = s.cust_sid)
when matched then update set 
    a.latest_fl = case when next_fl = 'Y' then 'N' else a.latest_fl end,
    a.effective_dt_to   = case when next_fl = 'Y' and  next_edt = a.effective_dt_to 
                                then next_run
                                else a.effective_dt_to end,
    a.effective_dt_from = case when prev_fl = 'Y' and  prev_edt = a.effective_dt_to 
                                then etl_job_run_day
                                else a.effective_dt_to end, 
    version = case when prev_fl = 'Y' then version + 1 else version end