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
 SQL Server Development (2000)
 Help with SELECT and COUNT

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:

FirstName
David
Joe
Klark
David
David
David
Joe
Joe

I 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
Hi

you mean DISTINCT and GROUP BY




-------------------------
R...
Go to Top of Page

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
Go to Top of Page

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.
Go to Top of Page

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-11-05 : 05:11:54
Hi


DECLARE @TABLE TABLE(FIRSTNAME VARCHAR(10))

INSERT INTO @TABLE
SELECT 'DAVID' UNION ALL
SELECT'JOE' UNION ALL
SELECT'KLARK' UNION ALL
SELECT'DAVID' UNION ALL
SELECT'DAVID' UNION ALL
SELECT'DAVID' UNION ALL
SELECT'JOE' UNION ALL
SELECT'JOE'


SELECT DISTINCT * FROM @TABLE

SELECT * FROM @TABLE GROUP BY FIRSTNAME



-------------------------
R...
Go to Top of Page

ynevet
Starting Member

10 Posts

Posted - 2009-11-05 : 06:29:57
Thank you all!

webfred's solution helped me.


Yair
Go to Top of Page

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.
Go to Top of Page
   

- Advertisement -