甲骨文 18c - 复杂 sql

Oracle 18c - Complex sql

我有一个 table 包含以下列:

Emp_ID Number
Emp_flag Varchar2(1)
Date_1 Date
Date_2 Date
create_date Date

本table无PK,Emp_id..

重复记录较多

我需要知道的是,什么时候输入新的 Date_1(所以日期为空,或者从日期 1 到日期 2)发生的日期。

我不能只看一条记录来比较 Date_1 和 create_date 因为给定 Emp_ID 的许多记录中有很多次 Date_1 只是“复制”到新记录。 Date_1 最初可能是在 02/15/2019 输入的,值为 02/01/2019。现在假设 Date_2 在 2020 年 2 月 12 日添加。所以 table 看起来像这样:

Emp_ID  Emp_flag    Date_1     Date_2      Create_Date
123     Y           Null       Null        1/18/2018
123     Y           02/1/2019  Null        02/15/2019
123     Y           02/1/2019  02/12/2021  02/12/2020

我需要一个 SQL 查询,它会告诉我 Emp_ID 123 在 2019 年 2 月 15 日输入了 Date_1 02/1/2019 并且没有收到任何其他记录。

预期输出:

Emp_ID  Emp_flag    Date_1     Date_2      Create_Date
123     Y           02/1/2019  Null        02/15/2019

例2(注意date_1不同):

Emp_ID  Emp_flag    Date_1     Date_2      Create_Date
456     Y           Null       Null        1/18/2018
456     Y           10/1/2019  Null        02/15/2019
456     Y           11/2/2019  02/12/2021  02/12/2020

预期输出:

Emp_ID  Emp_flag    Date_1     Date_2      Create_Date
456     Y           10/1/2019  Null        02/15/2019
456     Y           11/2/2019  02/12/2021  02/12/2020

示例 3:

Emp_ID  Emp_flag    Date_1     Date_2      Create_Date
456     Y           Null       Null        1/18/2018
456     Y           10/1/2019  Null        02/15/2019
456     Y           10/1/2019  Null        02/15/2019
456     Y           11/2/2019  02/12/2021  02/12/2020

预期输出:

Emp_ID  Emp_flag    Date_1     Date_2      Create_Date
456     Y           10/1/2019  Null        02/15/2019
456     Y           11/2/2019  02/12/2021  02/12/2020

示例 4:

Emp_ID  Emp_flag    Date_1     Date_2      Create_Date
456     Y           10/1/2019  Null        02/15/2019
456     Y           10/1/2019  Null        02/16/2019

预期输出:无记录。

您可以使用Lag函数来检查date_1的前一个值是否存在。

SELECT x.emp_id,
       x.date_1,
       x.create_date AS first_date_with_date_1
FROM (
    SELECT t.emp_id,
           t.create_date,
           t.date_1,
           LAG(t.date_1) OVER (PARTITION BY t.emp_id ORDER BY t.create_date) AS last_date_1
    FROM your_table t
) x
WHERE x.date_1 IS NOT NULL
  AND x.last_date_1 IS NULL

这里可以使用滞后函数代替超前函数:

with tableA as
(
select 456 as Emp_ID,'Y' as Emp_flag,CAST(NUll as date) as Date_1,CAST(NULL as date) as Date_2,CAST('18Jan2018' as date) as Create_date from dual union
select 456,'Y',CAST('01Oct2019' as date),Null,CAST('15Feb2019' as date) from dual union
select 456,'Y',CAST('02Nov2019' as date),CAST('12Feb2021' as date),CAST('12Feb2020' as date) from dual) 

select x.Emp_ID,x.Emp_flag,x.Date_1,x.Date_2,x.Create_date
from
(select a.*
       ,lag(a.date_1) Over (partition by a.Emp_ID order by a.create_date) as lag_date
from tableA a) x
where x.date_1 is not null and x.date_1<>COALESCE(x.lag_date,CAST('01Jan2100' as date))

这只会在 date_1 发生变化时给出值。由于 NULL 比较不起作用,我已将它们替换为 1/1/2100。希望这有帮助。

编辑:

我检查了您提到的样本,它似乎确实有效。如果它不起作用,请分享您的预期和结果:

with tableA as
(
select 456 as Emp_ID,'Y' as Emp_flag,CAST(NUll as date) as Date_1,CAST(NULL as date) as Date_2,CAST('18Jan2018' as date) as Create_date from dual union
select 456,'Y',CAST('01Oct2019' as date),Null,CAST('15Feb2019' as date) from dual union
select 456,'Y',CAST('01Oct2019' as date),CAST('12Feb2021' as date),CAST('12Feb2020' as date) from dual) 

select x.Emp_ID,x.Emp_flag,x.Date_1,x.Date_2,x.Create_date
from
(select a.*
       ,lag(a.date_1) Over (partition by a.Emp_ID order by a.create_date) as lag_date
from tableA a) x
where x.date_1 is not null and x.date_1<>COALESCE(x.lag_date,CAST('01Jan2100' as date))

测试所有情况:

with t(emp_id, emp_flag, date_1, date_2, create_date) as (
    select 101, 'Y', null,              null,              date '2018-01-18' from dual union all
    select 101, 'Y', date '2019-02-01', null,              date '2019-02-15' from dual union all
    select 101, 'Y', date '2019-02-01', date '2021-02-12', date '2019-02-16' from dual union all
    select 102, 'Y', null,              null,              date '2018-01-18' from dual union all
    select 102, 'Y', date '2019-02-10', null,              date '2019-02-15' from dual union all
    select 102, 'Y', date '2019-02-11', date '2021-02-12', date '2019-02-16' from dual union all
    select 103, 'Y', null,              null,              date '2018-01-18' from dual union all
    select 103, 'Y', date '2019-02-10', null,              date '2019-02-15' from dual union all
    select 103, 'Y', date '2019-02-10', null,              date '2019-02-15' from dual union all
    select 103, 'Y', date '2019-02-11', date '2021-02-21', date '2020-12-02' from dual )
select emp_id, emp_flag, date_1, date_2, create_date
  from (
    select emp_ID, emp_flag, date_1, date_2, create_date,
           lag(date_1) over (partition by emp_id order by create_date) prev_dt1
      from t )
  where date_1 <> nvl(prev_dt1, date_1 - 1);

结果:

    EMP_ID EMP_FLAG DATE_1      DATE_2      CREATE_DATE
---------- -------- ----------- ----------- -----------
       101 Y        2019-02-01              2019-02-15
       102 Y        2019-02-10              2019-02-15
       102 Y        2019-02-11  2021-02-12  2019-02-16
       103 Y        2019-02-10              2019-02-15
       103 Y        2019-02-11  2021-02-21  2020-12-02

编辑:

when there are more than one records with no change in Date_1. It should not return a record for that Emp_id

在这种情况下,date_1 设置在第一行 (id 104)。如果您想在这种情况下隐藏行,请使用:

with t(emp_id, emp_flag, date_1, date_2, create_date) as (
    select 104, 'Y', date '2019-02-10', null,              date '2019-02-15' from dual union all
    select 104, 'Y', date '2019-02-10', null,              date '2019-02-16' from dual union all
    select 105, 'Y', date '2019-02-10', null,              date '2019-02-15' from dual union all
    select 105, 'Y', null,              null,              date '2019-02-16' from dual )
select emp_id, emp_flag, date_1, date_2, create_date
  from (
    select emp_ID, emp_flag, date_1, date_2, create_date,
           lag(date_1) over (partition by emp_id order by create_date) prev_dt1, 
           row_number() over (partition by emp_id order by create_date) rn
      from t )
  where (date_1 is not null and prev_dt1 is null and rn > 1)
     or date_1 <> prev_dt1
     or date_1 is null and prev_dt1 is not null;

我还添加了前一个日期设置时的大小写,现在它为空 (id 105)。如果不可能或您不想要它,请删除最后一行。