【问题标题】:Removing rows from table with specific distinct values从具有特定不同值的表中删除行
【发布时间】:2015-08-28 03:14:10
【问题描述】:

所以我有这个示例表

+----+------------------+-------+------------+------------+--------+
| ID | KeyAccountNumber | GList | StartDate  |  EndDate   |  Rate  |
+----+------------------+-------+------------+------------+--------+
|  1 |              100 | ABCD  | 01-01-2015 | 01-30-2015 | 100.00 |
|  2 |              101 | ABECD | 02-01-2015 | 02-28-2015 | 105.00 |
|  3 |              100 | ABCD  | 01-01-2015 | 01-30-2015 | 107.00 |
+----+------------------+-------+------------+------------+--------+

我需要完成的是,对于给定的不同 'KeyAccountNumber' 和 'GList' 值,我需要删除 StartDate 和 EndDate 重复的位置(删除两行)。

所以在这种情况下,我需要删除 id 为 1 和 3 的行,因为它们在 StartDate 和 EndDate 字段中具有相同的 KeyAccountNumber 和相同的 GList 值以及相同的日期,即使 Rate 字段不同。

所以,输出应该是这样的:

+----+------------------+-------+------------+------------+--------+
| ID | KeyAccountNumber | GList | StartDate  |  EndDate   |  Rate  |
+----+------------------+-------+------------+------------+--------+
|  2 |              101 | ABECD | 02-01-2015 | 02-28-2015 | 105.00 |
+----+------------------+-------+------------+------------+--------+

最好的方法是什么?

谢谢!

【问题讨论】:

标签: sql sql-server tsql


【解决方案1】:
DELETE t
FROM YourTable t
Group By KeyAccountNumber,GList,StartDate,EndDate
HAVING COUNT(*) > 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    相关资源
    最近更新 更多