Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to display records based on count

Status
Not open for further replies.

konda1234

Programmer
Oct 28, 2004
5
US
Hi,

I have a table like this:

firstname lastname state zipcode

Chris1 clark1 AX 23433
Chris1 clark1 CA 24334
Chris1 clark1 TX 34344
Chris clark CA 44125
Chris clark OH 44124
Chris2 clark1 AX 23433
Chris2 clark1 CA 24334
Chris2 clark1 TX 34344
Chris2 clark1 AZ 34344

In the above table the firstname of Chris is repeating 2 times, Chris1 = 3 times, Chris2 = 4 times

Let say I want to display only firstname count between 2 and 3 i.e
I want output some thing like this in the query.

firstname lastname state zipcode

Chris1 clark1 AX 23433
Chris1 clark1 CA 24334
Chris1 clark1 TX 34344
Chris clark CA 44125
Chris clark OH 44124

Now in the above output 6th record (Chris2) is not displayed because Chris2 count = 4

How do I write a query that should filter based on firstname count(the count is parameter).

Any help please....

Thanks.

 
This query identifies the names that appear 2 or 3 times:

Select firstname from mytable group by firstname having count(firstname) in (2,3)

You then need a query to list all the records for names that appear in the first query.

Select * from mytable where firstname in (Select firstname, from mytable group by firstname having count(firstname) in (2,3))
 
Thank you very much for your help.
The way you told works perfect.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top