Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SELECT duplicates from different columns? HELP

Author  Topic 

akdom
Starting Member

1 Post

Posted - 2009-05-29 : 04:28:02



ID VALUE1 VALUE2
001 1000 7777
002 9999 7777
003 7777 3000
004 9999 4000
005 6000 9999

// I need to find the rows that have duplicates BOTH in VALUE1 and VALUE2. Could anyone tell me what the correct SQL statement should be?

so the output should be like:

duplicate FOUND ---> 7777
ID VALUE1 VALUE2
001 1000 7777
002 9999 7777
003 7777 3000

duplicate FOUND ---> 9999
ID VALUE1 VALUE2
002 9999 7777
004 9999 4000
005 6000 9999

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-05-29 : 04:35:51
[code]SELECT Value
FROM (
SELECT DISTINCT Value1 AS Value FROM Table UNION ALL
SELECT DISTINCT Value2 FROM Table
) AS d
GROUP BY Value
HAVING COUNT(*) > 1[/code]

E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

Dev@nlkss

134 Posts

Posted - 2009-05-29 : 04:44:58
select *
from <Table>
where case when v1 in(select distinct v2 from <Table>) then 1
when v2 in(select distinct v1 from <Table>) then 1 else 0 end=1

$atya.

Love All Serve All.
Go to Top of Page
   

- Advertisement -