简体   繁体   English

按日期分组查询

[英]Query with group by date

I'm working on the following user table, where role = 2 means the user is an instructor, and role = 3 means that the user is a student.我正在处理以下用户表,其中角色 = 2 表示用户是讲师,角色 = 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    |
+--------+------+---------------+

My goal is to select the sum value of all instructors and students, grouped by date.我的目标是 select 所有教师和学生的总和值,按日期分组。 The result should look like this:结果应如下所示:

+------------------+---------------+---------------+
| totalInstructors | totalStudents | creation_date |
+------------------+---------------+---------------+
|                1 |             2 | 2020-07-01    |
|                0 |             1 | 2020-08-01    |
+------------------+---------------+---------------+

In this case, on 2020-07-01, I had 1 instructor and 2 students registered and on 2020-08-01, I had no instructors and I had 1 student registered.在这种情况下,在 2020 年 7 月 1 日,我有 1 名教师和 2 名学生注册,而在 2020 年 8 月 1 日,我没有教师,我有 1 名学生注册。

My problem is that I am having difficulties in setting up this query, if someone can help me thank you very much!我的问题是我在设置此查询时遇到困难,如果有人可以帮助我,非常感谢!

Use conditional aggregation:使用条件聚合:

SELECT
    creation_date,
    COUNT(CASE WHEN role = 2 THEN 1 END) AS totalInstructors,
    COUNT(CASE WHEN role = 3 THEN 1 END) AS totalStudents
FROM yourTable
GROUP BY
    creation_date;

下面演示链接的屏幕截图

Demo 演示

You would need count with a case statement as follows您需要使用 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

A simple GRoupBY and SUM helps一个简单的 GRoupBY 和 SUM 有帮助

This works.这行得通。 because the compasison role = 2 gives 1 back it it tue and 0 if it is false因为同情角色 = 2 在星期二返回 1,如果为假则返回 0

 CREATE TABLE table1 ( `name` VARCHAR(6), `role` INTEGER, `creation_date` VARCHAR(10) ); INSERT INTO table1 (`name`, `role`, `creation_date`) VALUES ('Tom', '2', '2020-07-01'), ('Diana', '3', '2020-07-01'), ('Rachel', '3', '2020-07-01'), ('Michel', '3', '2020-08-01');
 SELECT SUM(`role` = 2) totalInstructors, SUM(`role` = 3) totalStudents, `creation_date` FROM table1 GROUP BY `creation_date` ORDER BY `creation_date`
 totalInstructors |总教员 | totalStudents |学生总数 | creation_date ---------------: |创建日期 ---------------: | ------------: |:------------ 1 | ------------: |:------------ 1 | 2 | 2 | 2020-07-01 0 | 2020-07-01 0 | 1 | 1 | 2020-08-01 2020-08-01

db<>fiddle here db<> 在这里摆弄

Please use below query,请使用以下查询,

select 
case when role = 2 then count(1) end as totalInstructors,
case when role = 3 then count(1) end as totalStudents,
creation_date
from table_name
group by  creation_date;

You can use COALESCE() to replace null with 0您可以使用COALESCE()将 null 替换为 0

select COALESCE(totalInstructors, 0) as totalInstructors, COALESCE(totalStudents, 0) as totalStudents,creation_date
from
(select 
case when role = 2 then count(1) end as totalInstructors,
case when role = 3 then count(1) end as totalStudents,
creation_date
from table_name
group by  creation_date) qry;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM