Tom Lima
Tom Lima

Reputation: 1056

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.

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

My problem is that I am having difficulties in setting up this query, if someone can help me thank you very much!

Upvotes: 1

Views: 44

Answers (4)

nbk
nbk

Reputation: 49410

A simple GRoupBY and SUM helps

This works. because the compasison role = 2 gives 1 back it it tue and 0 if it is false

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 |             2 | 2020-07-01   
               0 |             1 | 2020-08-01   

db<>fiddle here

Upvotes: 1

Jim Macaulay
Jim Macaulay

Reputation: 5165

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

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;

Upvotes: 0

George Joseph
George Joseph

Reputation: 5932

You would need count with a case statement as follows

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

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522731

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;

screen capture from demo link below

Demo

Upvotes: 1

Related Questions