tags:

views:

113

answers:

2

Basic SQL statement question -

I have a table (myUsers) that contains the column "UserID." The same UserID can appear one to many times in these rows. I'm looking for a query that will give me back the specific userIDs that appear the most in this table, and also with their count. Any thoughts?

Thanks in advance!

+13  A: 
select UserID, count(UserID)
from myUsers
group by UserID
order by count(UserID) desc
David Kemp
would change to ...order by count(userid) DESC
Ed Schembor
throw a *having* clause in there and you're done
kdgregory
@kdgregory yeah, but I don't know what the magic number is
David Kemp
@Ed - you saw the post whilst I was editing - still +1 to you
David Kemp
thanks for the quick response! worked like a champ.
Alex
+1  A: 
DECLARE @THRESHOLD INT
SET @THRESHOLD = 20
SELECT UserID, COUNT(*)
FROM MYUSERS
GROUP BY UserID
HAVING COUNT(*) > @THRESHOLD
ORDER BY COUNT(*) DESC

EDIT: I changed from where to having, duh totally forgot about that. :)

Wil P