开发者

SQL select statement to find ID's that occur the most

开发者 https://www.devze.com 2022-12-13 03:08 出处:网络
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 bac

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 user开发者_Python百科IDs that appear the most in this table, and also with their count. Any thoughts?

Thanks in advance!


select UserID, count(UserID)
from myUsers
group by UserID
order by count(UserID) desc


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. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消