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
 how to find same data in a column?

Author  Topic 

allan8964
Posting Yak Master

249 Posts

Posted - 2012-02-29 : 10:30:50
Hi there,

A table col has same data like this:

ID | Code
----------
22 | CBD
31 | FDC
43 | CBD
45 | FDC


How can can I find how many rows have same data. I don't mean script like this:
select * from table where code = 'CBD'

or

select * from table where code in ('CBD', 'FDC' ...)

I mean sort out all the rows which all have same data.
Thanks in advance.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-02-29 : 10:35:36
select t1.*
from table t1
join (select Code from table group by Code having count(*) > 1)t2
on t2.Code = t1.Code


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 2012-02-29 : 10:53:17
Thank you soooooooooooooooooo much!!!
This is what I need. Thank you!!!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-29 : 11:20:52
Why not just:

SELECT Code, COUNT(*) AS Repeat_Rows
FROM table
GROUP BY Code
HAVING COUNT(*) > 1

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-02-29 : 11:32:03
quote:
Originally posted by X002548

Why not just:

SELECT Code, COUNT(*) AS Repeat_Rows
FROM table
GROUP BY Code
HAVING COUNT(*) > 1

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/





I think the OP wants to see all rows and not only a rowcounter.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 2012-02-29 : 11:50:22
Yes, you both are right. Bret's script gives the sum and actually equals the part of webfred's. Pulling out the t2 from webfred and adding count() will get same as Bret's:

select Code, Count(Code) As Repeat_Rows from table group by Code having count(*) > 1
Go to Top of Page
   

- Advertisement -