SQL计数和组 [英] SQL count and group

查看:68
本文介绍了SQL计数和组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的MySQL-db表:

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

如何使用SQL做到这一点?我想我需要盘点,分组,也许还需要一些加入,但我不知道该怎么做.

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.

推荐答案

您可以使用条件聚合:

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;

这在MySQL中使用了简写形式,将布尔表达式视为整数.上面的每个sum()都等同于sum(case when class = 1 then 1 else 0 end)之类的东西.

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).

这篇关于SQL计数和组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆