我正在使用以下用户表,其中role = 2表示该用户是一名教师,而role = 3表示该用户是一名学生。
+--------+------+---------------+
| name | role | creation_date |
+--------+------+---------------+
| Tom | 2 | 2020-07-01 |
| Diana | 3 | 2020-07-01 |
| Rachel | 3 | 2020-07-01 |
| Michel | 3 | 2020-08-01 |
+--------+------+---------------+
我的目标是按日期分组选择所有教师和学生的总和。结果应如下所示:
+------------------+---------------+---------------+
| totalInstructors | totalStudents | creation_date |
+------------------+---------------+---------------+
| 1 | 2 | 2020-07-01 |
| 0 | 1 | 2020-08-01 |
+------------------+---------------+---------------+
在这种情况下,我在2020-07-01年注册了1名教员和2名学生,而在2020-08-01年,我没有注册教员,并且有1名学生注册。
我的问题是,如果有人可以帮助我,我很难设置此查询,谢谢!
您将需要通过一个case语句进行计数,如下所示
select count(case when role=2 then 1 end) as totalInstructors
,count(case when role=3 then 1 end) as totalStudents
,creation_date
from tbl
group by creation_date
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句