使用NULLS和MAX VALUE设计重复数据删除sql语句

时间:2016-08-29 04:35:45

标签: sql sql-server

我试图雕刻一个SQL语句来重复表格。

该表有三个键:key1,key2,key3用作业务键。 也正在使用日期。

规则是(假设在key1,key2,key3中匹配):

If all rows have dates, retain only MAX(date)
If one row has a date, and others are NULL, retain only row with date
If all rows has date = NULL, keep all rows.

我一直在使用此代码作为基础:

WITH CTE AS(
   SELECT [key1], [key2], [key3], [date],
       RN = ROW_NUMBER()OVER(PARTITION BY [key1], [key2], [key3], [date] ORDER BY [date] desc)
   FROM dbo.Table1
)
DELETE FROM CTE WHERE RN > 1

我没有受过如何在sql语句中应用规则的教育。任何智慧都将受到高度赞赏。

重复数据删除示例:

CASE 1: before dedupication:            
key1    key2    key3    date
1   A   1   null
1   A   1   null
1   A   1   null

after deduplication:            
key1    key2    key3    date
1   A   1   null
1   A   1   null
1   A   1   null

CASE 2: before dedupication:            
key1    key2    key3    date
1   A   1   1/1/2016
1   A   1   1/1/2016
1   A   1   1/1/2016

after deduplication:            
key1    key2    key3    date
1   A   1   1/1/2016

CASE 3: before dedupication:            
key1    key2    key3    date
1   A   1   1/1/2016
1   A   1   1/2/2016
1   A   1   1/3/2016

after deduplication:            
key1    key2    key3    date
1   A   1   1/3/2016

CASE 4: before deduplication            
1   A   1   1/1/2016
1   A   1   1/1/2016
1   A   1   null

after deduplication:            
key1    key2    key3    date
1   A   1   1/1/2016


CASE 5: before deduplication            
1   A   1   1/1/2016
1   A   1   1/2/2016
1   A   1   null

after deduplication:            
key1    key2    key3    date
1   A   1   1/2/2016

2 个答案:

答案 0 :(得分:0)

我相信,你几乎需要一个WHERE来排除满足规则#3的行。

 ;WITH CTE AS(
        SELECT [key1], [key2], [key3], [date],
           RN = ROW_NUMBER()OVER(PARTITION BY [key1], [key2], [key3] ORDER BY isnull([date], '19000101' desc)
        FROM dbo.Table1 t1
        WHERE EXISTS ( SELECT * 
                       FROM dbo.Table1 t2 
                       WHERE t1.key1=t2.key1 
                         and t1.key2=t2.key2 
                         and t1.key3=t2.key3 
                         and t1.[date] IS NOT NULL
                     )
    )
    DELETE FROM CTE WHERE RN > 1

答案 1 :(得分:0)

class Category < ApplicationRecord
  has_many :posts
end