BluePrint
BluePrint

Reputation: 2134

SQL count and group

I have a MySQL-db table looking similar to this:

id  date        class   more_info_one   more_info_two   etc
1   2017-05-03  1       …               …               …
2   2017-05-03  3       …               …               …
3   2017-05-11  1       …               …               …
4   2017-05-05  1       …               …               …
5   2017-05-12  2       …               …               …
6   2017-05-05  1       …               …               …
7   2017-05-04  2       …               …               …
8   2017-05-06  3       …               …               …
9   2017-05-12  3       …               …               …
10  2017-05-07  2       …               …               …
11  2017-05-09  1       …               …               …
12  2017-05-09  3       …               …               …
13  2017-05-11  1       …               …               …
14  2017-05-09  1       …               …               …
15  2017-05-04  2       …               …               …
16  2017-05-10  1       …               …               …
17  2017-05-03  2       …               …               …
18  2017-05-12  3       …               …               …
19  2017-05-12  3       …               …               …
20  2017-05-06  2       …               …               …
21  2017-05-03  1       …               …               …
22  2017-05-09  3       …               …               …
23  2017-05-06  1       …               …               …
24  2017-05-08  1       …               …               …
25  2017-05-11  2       …               …               …
26  2017-05-03  1       …               …               …
27  2017-05-09  2       …               …               …
28  2017-05-06  3       …               …               …
29  2017-05-12  3       …               …               …
30  2017-05-06  2       …               …               …
31  2017-05-07  1       …               …               …
32  2017-05-11  3       …               …               …
33  2017-05-07  1       …               …               …
34  2017-05-06  1       …               …               …
35  2017-05-08  2       …               …               …
36  2017-05-11  1       …               …               …
37  2017-05-12  2       …               …               …
38  2017-05-06  3       …               …               …
39  2017-05-05  3       …               …               …
40  2017-05-07  2       …               …               …

I want to find how many of each class for each date so I get the following result:

date        class_one   class_two   class_three
2017-05-03  3           1           1
2017-05-04  0           2           0
2017-05-05  2           0           3
2017-05-06  2           2           3
2017-05-07  2           2           0
2017-05-08  1           1           0
2017-05-09  2           1           2
2017-05-10  1           0           0
2017-05-11  3           1           1
2017-05-12  0           2           4

How can I do this using SQL? I am guessing that I need to count, group by and maybe some join, but I can't figure out how.

Upvotes: 1

Views: 106

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269463

You can use conditional aggregation:

select date, sum(class = 1) as class_1, sum(class = 2) as class_2,
       sum(class = 3) as class_3
from t
group by date
order by date;

This uses a short-hand in MySQL that treats boolean expressions as integers. Each sum() above is equivalent to something like sum(case when class = 1 then 1 else 0 end).

Upvotes: 4

Related Questions