需要删除重复的数据

时间:2015-10-13 08:57:28

标签: sql sql-server

下面是我通过两个临时表之间的简单连接设法获得的表。现在预期的结果有点难以解释,我甚至不知道从哪里开始。因此,我将发布预期结果并回答可能出现的任何问题。

修改:两个列中的每个日期基本上只需要出现一次。

php app/console generate:doctrine:entities AcmeTaskBundle:Task

预期结果

+----+------------------+----------------+----------------------+-----------     ---------+-----------------------+---------------------+
| ID | Date from Source | Date to source | From Source Revision | To source Revision | From Source InstantID | To Source InstantID |
+----+------------------+----------------+----------------------+--------------------+-----------------------+---------------------+
| 45 | 2015-06-19       | 2015-07-06     | P0                   | P0                 |                     1 |                   3 |
| 45 | 2015-06-19       | 2015-07-09     | P0                   | P0                     |                     1 |                   5 |
| 45 | 2015-07-27       | 2015-07-06     | P0                   | P0                 |                     6 |                   3 |
| 45 | 2015-07-27       | 2015-07-09     | P0                   | P0                 |                     6 |                   5 |
| 45 | 2015-07-28       | 2015-07-06     | P0                   | P0                 |                     7 |                   3 |
| 45 | 2015-07-28       | 2015-07-09     | P0                   | P0                 |                     7 |                   5 |
| 45 | 2015-07-30       | 2015-07-06     | P0                   | P0                 |                     8 |                   3 |
| 45 | 2015-07-30       | 2015-07-09     | P0                   | P0                  |                     8 |                   5 |
+----+------------------+----------------+----------------------+--------------------+-----------------------+---------------------+

SQL Fiddle

1 个答案:

答案 0 :(得分:0)

试试这个:

DELETE FROM myTable WHERE Id IN (
    SELECT Id FROM (SELECT Id
   ,ROW_NUMBER() OVER (PARTITION BY [Date from Source] ORDER BY [Date from Source]) AS [rn]             
        FROM myTable 
    ) a WHERE rn > 1 
)