tags:

views:

206

answers:

5

I have a submission table that is very simple: userId, submissionGuid

I want to select the username (simple inner join to get it) of all the users who have more than 10 submissions in the table.

I would do this with embedded queries and a group by to count submissions... but is there a better way of doing it (without embedded queries)?

Thanks!

+1  A: 
select userId, count(*)
from   submissions
having count(*) > 10
group by userId
cagcowboy
+4  A: 

This is the simplest way, I believe:

select userId
from submission   
group by userId
having count(submissionGuid) > 10
Blorgbeard
Thanks. That's definitely cleaner than doing: select * from (select count(1) as subs, userid from submissions group by userid) where subs > 10(which is the way I would have thought to do it.)
rksprst
+1  A: 
SELECT 
    username 
FROM 
    usertable 
    JOIN submissions 
     ON usertable.userid = submissions.userid 
GROUP BY 
    usertable.username 
HAVING 
    Count(*) > 1

*Assuming that your "Users" table is call usertable and that it has a column called "UserName"

Darksider
A: 

I think the correct query is this (SQL Server):

SELECT s.userId, u.userName
FROM submission s INNER JOIN users u on u.userId = s.userId   
GROUP BY s.userId, u.username
HAVING COUNT(submissionGuid) > 10

If you don't have the HAVING clause:

SELECT u.userId, u.userName
FROM users u INNER JOIN (
    SELECT userId, COUNT(submissionGuid) AS cnt
    FROM submission
    GROUP BY userId ) sc ON sc.userId = u.userId
WHERE sc.cnt > 10
Sklivvz
A: 

select userid, count(submissionGUID) as submitCount

from Submissions

group by userid, submitCount

having submitCount > 10