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.
Author |
Topic |
ynevet
Starting Member
10 Posts |
Posted - 2009-11-05 : 04:58:56
|
Hi folks,I would like to return only the rows where the value of theirs {ColumnName} column repeats in X rows or More in my table.For example, from the following table I want to return the FirstNames David and Joe because they repeats 3 or more times in the table rows:FirstNameDavidJoeKlarkDavidDavidDavidJoeJoeI hope you can help me with the SQL script.Thank you!Yair |
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-11-05 : 05:05:29
|
Hiyou mean DISTINCT and GROUP BY-------------------------R... |
|
|
ynevet
Starting Member
10 Posts |
Posted - 2009-11-05 : 05:07:57
|
Maybe if you said.Do you know what is the correct query to get the desired result?Yair |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-11-05 : 05:11:15
|
select Firstname, count(*) as [Count] from table group by Firstname having count(*) >= 3 No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-11-05 : 05:11:54
|
HiDECLARE @TABLE TABLE(FIRSTNAME VARCHAR(10))INSERT INTO @TABLESELECT 'DAVID' UNION ALLSELECT'JOE' UNION ALLSELECT'KLARK' UNION ALLSELECT'DAVID' UNION ALLSELECT'DAVID' UNION ALLSELECT'DAVID' UNION ALLSELECT'JOE' UNION ALLSELECT'JOE' SELECT DISTINCT * FROM @TABLESELECT * FROM @TABLE GROUP BY FIRSTNAME -------------------------R... |
|
|
ynevet
Starting Member
10 Posts |
Posted - 2009-11-05 : 06:29:57
|
Thank you all!webfred's solution helped me.Yair |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-11-05 : 07:24:28
|
welcome No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
|
|
|