Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Distinct Count with Group By

My table in this issue has three columns: Player, Team and Date_Played.

Player         Team         Date_Played
John Smith     New York     2/25/2014
Joe Smith      New York     2/25/2014
Steve Johnson  New York     2/25/2014
Steph Curry    Orlando      2/25/2014
Frank Anthony  Orlando      2/26/2014
Brian Smith    New York     2/26/2014
Steve Johnson  New York     2/27/2014
Steph Curry    New York     2/28/2014

I know how to get a list of distinct team names or dates played, but what I'm looking for is a query that will group by Team and count the number of distinct Team & Date combinations. So the output should look like this:

Team          Count
New York      4
Orlando       2

So far I have:

SELECT DISTINCT Tm, Count(Date_Played) AS Count
FROM NBAGameLog
GROUP BY NBAGameLog.[Tm];

But this gives me the total amount of records grouped by date.

like image 704
fullOfQuestions Avatar asked Mar 01 '14 15:03

fullOfQuestions


1 Answers

A bit less code:

SELECT Team, Count(distinct Date_Played) AS Count 
FROM NBAGameLog 
GROUP BY Team

It does Show the same execution plan as the first answer.

like image 107
nicko66 Avatar answered Oct 03 '22 06:10

nicko66