删除具有不同日期条目的重复数据,保留最早的日期条目

时间:2017-10-16 09:20:53

标签: sql sql-server

我有一个表格,其中包含一些我想删除的错误数据。

逻辑如下。代码,名称和来源相同的地方(如下表中的前三行我想保留最早的日期记录,所以2017-01-02在这种情况下然后删除另外两行。怎么做此?

   DateEntry    Code    Name     Source
   2017-01-02   ABCF    Blah     SS
   2017-03-02   ABCF    Blah     SS
   2017-04-02   ABCF    Blah     SS       
   2017-03-08   BVFT    Andy     SS
   2017-04-14   BVFT    Andy     SS
   2017-04-20   BVFT    Blob     SS
   2017-04-28   BVFT    Blob     SS
   2017-05-14   BVFT    Blob     SS
   2017-06-02   BVFT    Blob     SS

我想要的,

   DateEntry    Code    Name     Source
   2017-01-02   ABCF    Blah     SS  
   2017-03-08   BVFT    Andy     SS
   2017-04-20   BVFT    Blob     SS

6 个答案:

答案 0 :(得分:1)

提供按列CodeNameSource分区的行号,并按DateEntry列的升序排序。然后删除行号大于1的行。

<强>查询

;with cte as(
    select [rn] = row_number() over(
        partition by Code, Name, Source
        order by DateEntry    
     ), *
     from your_table_name
)
delete from cte
where rn > 1;

<强> Find a demo here

答案 1 :(得分:1)

您应该ROW_NUMBER()SubQuery表格运作:

SELECT DateEntry,
       Code,
       Name,
       Source
FROM
(
    SELECT *,
           ROW_NUMBER() OVER(PARTITION BY Code,
                                          Name,
                                          Source ORDER BY dateentry) rn
    FROM <table_name>
) a
WHERE rn = 1;

结果:

|  DateEntry | Code | Name | Source |
|------------|------|------|--------|
| 2017-01-02 | ABCF | Blah |     SS |
| 2017-03-08 | BVFT | Andy |     SS |
| 2017-04-20 | BVFT | Blob |     SS |

答案 2 :(得分:0)

您可以将排名功能ROW_NUMBERPARTITION BY Code, Name

一起使用
WITH Ranked 
AS
(
   SELECT
      DateEntry, Code,Name, Source,
      ROW_NUMBER() OVER(PARTITION BY Code, Name ORDER BY DateEntry) AS RN
   FROM table1
)
SELECT DateEntry, Code,Name, Source
FROM Ranked
WHERE RN = 1;

排名功能将为每一行生成一个行号,组中的第一行为1,即具有最早日期的行。然后过滤器将删除除每组中第一行之外的所有行。

结果:

|  DateEntry | Code | Name | Source |
|------------|------|------|--------|
| 2017-01-02 | ABCF | Blah |     SS |
| 2017-03-08 | BVFT | Andy |     SS |
| 2017-04-20 | BVFT | Blob |     SS |

请注意,这实际上删除其他行,这只是一个选择。

答案 3 :(得分:0)

SELECT * FROM (
SELECT 
    *,
    ROW_NUMBER() OVER(PARTITION BY DateEntry ORDER BY DateEntry DESC) AS rn
FROM MyTable
) a
WHERE rn = 1

答案 4 :(得分:0)

试试这个

;with cte as (
SELECT *,ROW_NUMBER() OVER(PARTITION BY Code,Name,Source ORDER BY DateEntry DESC) AS rnum
FROM MyTable
)
delete from cte where rnum>1

答案 5 :(得分:0)

WITH t1 AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY Code,Name,Source ORDER BY DateEntry) AS   
'Rank',Code,Name,Source 
FROM TABLE
)
DELETE FROM t1 WHERE t1.Rank>1