Oliver Leopold
Oliver Leopold

Reputation: 51

Group By Multiple Columns Separately

I know that you can use the GROUP BY keyword to group by multiple columns, but here's what I'm trying to do:

Table: Codes

user_id    day_of_week    id
------------------------------
  1          T             1
  1          W             2
  1          W             3
  2          F             4
  2          F             5
  2          M             6

And I am trying to find a command to output to get this:

Table: Codes

user_id    day_of_week    count(*)
-------------------------------------
  1          T              1
  1          W              2
  2          F              2
  2          M              1

So as you can see, it's grouping by the day of the week and the user_id Can someone help me achieve this with MySQL?

Upvotes: 0

Views: 183

Answers (1)

Lukasz Szozda
Lukasz Szozda

Reputation: 176224

You can use multiple columns in GROUP BY clause:

SELECT user_id, day_of_week, COUNT(*) AS cnt
FROM Codes
GROUP BY user_id, day_of_week

Upvotes: 3

Related Questions