【问题标题】:SQL Server: remove duplicates in exchangeable columnsSQL Server:删除可交换列中的重复项
【发布时间】:2019-01-19 17:19:05
【问题描述】:

我有这个问题:

select 
    OSFES97.CodeId, OSFBA97.CodeId, OSFES97.ReceiveDate
from 
    StockArchives OSFES97 
inner join 
    StockArchives OSFBA97 on OSFBA97.ReceiveDate = OSFES97.ReceiveDate
where 
    OSFES97.CodeId <> OSFBA97.CodeId 

它返回如下结果:

CodeId    CodeId    ReceiveDate
------------------------------------------
1         2         2019-01-13 15:55:20.537
2         1         2019-01-13 15:55:20.537
1         2         2019-01-13 15:55:30.537
2         1         2019-01-13 15:55:30.537

由于我使用的以下记录被认为是重复的(如果它们具有相同的ReceiveDate),我想删除其中一个,并达到此结果:

CodeId    CodeId    ReceiveDate
------------------------------------------
1         2         2019-01-13 15:55:20.537
1         2         2019-01-13 15:55:30.537

【问题讨论】:

    标签: sql sql-server join select


    【解决方案1】:

    使用&lt;&gt; 运算符将创建这些(逻辑)副本。相反,您可以决定要使用较低 ID 的列,并相应地使用 &lt;&gt;。例如:

    SELECT     OSFES97.CodeId, OSFBA97.CodeId, OSFES97.ReceiveDate
    FROM       StockArchives OSFES97
    INNER JOIN StockArchives OSFBA97 ON OSFBA97.ReceiveDate = OSFES97.ReceiveDate
    WHERE       OSFES97.CodeId < OSFBA97.CodeId 
    -- Here -------------------^
    

    【讨论】:

    • 很好的解决方案,如果你知道我尝试了多么奇怪的事情,你会笑的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    相关资源
    最近更新 更多