Skip to content
Advertisement

SQL Server: remove duplicates in exchangeable columns

I have this query:

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

It returns a result like this:

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

As the following records for my use considered duplicate (if they have same ReceiveDate), I want to remove one of them, and achieve this result:

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

Advertisement

Answer

Using the <> operator will create these (logical) duplicates. Instead, you can decide which column you want to have the lower ID and use < or > accordingly. E.g.:

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 -------------------^
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement