【问题标题】:Comparing Multiple Dates to Identify Duplicate Records in a Database比较多个日期以识别数据库中的重复记录
【发布时间】:2021-12-16 07:35:24
【问题描述】:

我检查并发现了大量查找重复记录的线程,但没有任何专门解决日期比较的线程。

我正在尝试使用 JOIN 来识别数据库中哪里有类似的记录。其中一些记录在某些字段中具有相同的值,但不是全部。因此,我还想考虑与其他记录相同或差异为 +1 或 +2 的日期。

所以我对“重复”的定义是Color_Type、ID_Type和Category_Cd都相同,Activity_Date必须与相似记录相差0-2天。

例子:

**Record 1** has a 
Activity_Date of '10/25/2021' 
Color_Type of 'Blue' 
ID_Type as '1234' Category_Cd of 'Rainbow' 
Txn_ID of '1A2B'

**Record 2** has a 
Activity_Date of '10/24/2021' 
Color_Type of 'Blue' 
ID_Type as '1234' 
Category_Cd of 'Rainbow' 
Txn_ID of '3C4D'

**Record 3** has a 
Activity_Date of '10/26/2021' 
Color_Type of 'Blue' 
ID_Type as '1234' 
Category_Cd of 'Rainbow' 
Txn_ID of '5E6F'

**Record 4** has a 
Activity_Date of '09/01/2021' 
Color_Type of 'Blue' 
ID_Type as '1234' 
Category_Cd of 'Rainbow' 
Txn_ID of '7G8H'

**Record 5** has a 
Activity_Date of '10/25/2021' 
Color_Type of 'Blue' 
ID_Type as '1234' 
Category_Cd of 'Rainbow' 
Txn_ID of '9I0J'

我希望我的查询返回记录 1、2、3 和 5,但不返回记录 4

这是我的查询,用于查找重复项,但返回的记录相似但未考虑我想要的日期差异。

SELECT *
FROM 
    (SELECT * 
    FROM DATABASETABLE.DATABASEVIEW
    WHERE ACTIVTY_DT GE '2021-10-01'
    ) dupa  
JOIN 
    (SELECT ID_Type, Color_Type, Category_Cd, 
            COUNT (*) AS postings
    FROM DATABASETABLE.DATABASEVIEW
    WHERE ACTIVTY_DT GE '2021-10-01'
    GROUP BY 1, 2, 3
    HAVING postings = 2) dupb

    ON  dupa.ID_Type = dupb.ID_Type AND
        dupa.Color_Type = dupb.Color_Type AND
        dupa.Category_Cd = dupb.Category_Cd

ORDER BY dupa.ID_Type

对于 SQL 来说真的很新(并且是自学的),所以任何帮助都将不胜感激!!!

【问题讨论】:

  • 那个查询不能工作,从最后一个ON子句中去掉最后一个AND
  • 在您对表格的描述中没有提到ACTIVTY_DT
  • 我正在使用 Teradata。我看到当我创建我的问题时,表单会自动填充 mysql 标签。我会更新的。
  • Activity_Date must be within 0-2 days difference from similar records - 这不清楚。在最早记录返回的 2 天内?从上一行开始(基于 activity_date)?

标签: teradata


【解决方案1】:

这似乎是您基于 Windowed Aggregates 的逻辑

SELECT * 
FROM DATABASETABLE.DATABASEVIEW
WHERE ACTIVTY_DT >= DATE '2021-10-01'
QUALIFY -- current date within two days of previous date
   ACTIVTY_DT - 2
   <= 
   lag(ACTIVTY_DT)  -- prev date
   over (partition by ID_Type, Color_Type, Category_Cd -- same values in all three columns
         order by ACTIVTY_DT) 
or   -- similar condition against next row to get both rows retunrd
   ACTIVTY_DT + 2
   >= 
   lead(ACTIVTY_DT) -- next date
   over (partition by ID_Type, Color_Type, Category_Cd -- same values in all three columns
         order by ACTIVTY_DT) 

【讨论】:

    【解决方案2】:

    这是一个使用好的旧子查询的可能答案:

    SELECT * 
    FROM DATABASETABLE.DATABASEVIEW dupa
    WHERE dupa.ACTIVITY_DT GE '2021-10-01'
        and (select count(*) 
             from DATABASETABLE.DATABASEVIEW dupb
             where  dupb.ACTIVITY_DT GE '2021-10-01'
                    dupa.ID_Type = dupb.ID_Type AND
                    dupa.Color_Type = dupb.Color_Type AND
                    dupa.Category_Cd = dupb.Category_Cd AND
                    ABS(dupa.ACTIVITY_DT - dupb.ACTIVITY_DT) <= 2
            ) > 1
    

    对于dupa中的每条记录,我们统计dupb中Id_Type、Color_type、Category_cd和activity_dt相同的记录,最多允许有两天的差异,如果满足这个的记录数大于1则返回这个dupa 行(子查询也计算 dupa 记录本身,其结果始终 >=1)

    【讨论】:

      猜你喜欢
      • 2013-08-17
      • 1970-01-01
      • 2010-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-09
      相关资源
      最近更新 更多