views:

564

answers:

1

I have two tables: a 'userlist' that has a 'grade' and 'userID' columns, and a 'login' table that records each time a user is logged in. The login table has a 'userID' column that is associated with the 'userID'. This login table has a row inserted into it each time a user is logged in, so if a user logs in three times, three rows will be added to that table.

I need to run a query that returns how many unique logins happened per grade.

Ie: grade0 = 20 logins, grade1 = 30 logins

I have a feeling it's a combination of DISTINCT, COUNT and GROUP BY, but I can't get it right...

This query is wrong:

SELECT DISTINCT(userID), COUNT(*) as count, u.grade FROM userlist as u, login as l WHERE u.userID = l.userID GROUP BY u.grade, u.userID

It is not returning unique logins, but all logins (ie, if a user is logged in twice, he is counted twice).

A: 
select u.grade
     , count(distinct l.userid)
  from userlist u
  join login l
    on l.userid = u.userid
group
    by u.grade
longneck
Perfect! That does exactly what I needed. Thanks.
Khuffie