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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Selecting Duplicates question!

Author  Topic 

Gopher
Yak Posting Veteran

83 Posts

Posted - 2006-09-26 : 10:03:40
Hi All

If I have a table which contains duplicate data for example

Table1

Col1 Col2 Col3 Col4
---- ---- ---- ----
001 PGG 256 3256
631 PGG 256 3256
236 PGF 258 3258
063 PGF 258 3258
985 SGG 356 1116
121 SGG 356 1116

Col2, Col3, Col4 wil have the same data the only difference is the Col1 entry.

Is there a statement I can use to identify which items are duplicate and what there numbers are? So I end up with a statement that will tell me what the duplicates.
e.g

001,631,PGG,256,3256


Is this possible?

Any help would be great - Thanks in advance

Gopher

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-09-26 : 10:34:51
Select * from table
Where col1 in (select col1 from table group by col2,col3,col4 having count(*)>1)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Fromper
Starting Member

33 Posts

Posted - 2006-09-26 : 11:05:06
quote:
Originally posted by madhivanan

Select * from table
Where col1 in (select col1 from table group by col2,col3,col4 having count(*)>1)

Madhivanan

Failing to plan is Planning to fail


??? This seems wrong to me. How can your subquery select col1 when you're grouping on other fields? I thought grouping has to be done only on selected fields.

How about something like this?

SELECT col1 FROM table
INNER JOIN
(SELECT col2 as d2, col3 as d3, col4 as d4
FROM table
GROUP BY col2, col3, col4
HAVING COUNT(*) > 1 )
ON (col2 = d2 AND col3 = d3 AND col4 = d4)

--Richard
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-09-26 : 12:15:48
That should be min(col1)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -