views:

184

answers:

1

Newbie SQL question here -->

I've got an Occurrences table that contains a row for each time a user did something. A user can do the thing multiple times per day. It looks like this:

Date      Username
------    --------
1/1/9     User1
1/1/9     User1
1/1/9     User2
1/2/9     User1
1/2/9     User3
1/3/9     User1
1/3/9     User1
1/3/9     User1
1/3/9     User2
1/3/9     User3

I want a query to simply return the Username and the quantity (count) of unique days they appear. For the above data set, the result I'm looking for would be:

Username    UniqueDaysAppeared
--------    ------------------
User1       3
User2       2
User3       2

I keep getting screwed up because my query is returning not the count of unique days per user but rather the number of occurrences of the user overall.

+9  A: 
SELECT Username, COUNT(DISTINCT(Date)) AS UniqueDaysAppeared
FROM Occurrences
GROUP BY Username
AdaTheDev
As they say in those Staples commercials, "That was easy!" In all of my attempts I'd omitted the DISTINCT from the COUNT aggregate. Thank you.
Robert
And as Tommy Cooper would say "Just like that!" :)
AdaTheDev