I have a database that is set up with a date and a start and end time
date | start | end |
---|---|---|
2021-12-04 | 10:15:00 | 13:05:00 |
2021-12-04 | 12:18:00 | 15:38:00 |
... | ... | ... |
This table saves times people enter or leave.
For a statistic I want to get the number of people who were clocked in for every hour
For example if I specify date "2021-12-04" the table should look like this:
hour | people |
---|---|
10:00-10:59 | 1 |
11:00-11:59 | 1 |
12:00-12:59 | 2 |
13:00-13:59 | 2 |
14:00-14:59 | 1 |
15:00-15:59 | 1 |
I have been trying some queries and I get tables returning the number correctly for one hour, but as soon as it comes to every hour someone has been clocked in I can't seem to find a solution that works as intended
checking for each hour, combining with union:
select '00:00-00:59' as hour, count(date) as people from table1
where '00' between substr(start,1,2) and substr(end,1,2) union
select '01:00-01:59' as hour, count(date) as people from table1
where '01' between substr(start,1,2) and substr(end,1,2) union
select '02:00-02:59' as hour, count(date) as people from table1
where '02' between substr(start,1,2) and substr(end,1,2) union
select '03:00-03:59' as hour, count(date) as people from table1
where '03' between substr(start,1,2) and substr(end,1,2) union
select '04:00-04:59' as hour, count(date) as people from table1
where '04' between substr(start,1,2) and substr(end,1,2) union
select '05:00-05:59' as hour, count(date) as people from table1
where '05' between substr(start,1,2) and substr(end,1,2) union
select '06:00-06:59' as hour, count(date) as people from table1
where '06' between substr(start,1,2) and substr(end,1,2) union
select '07:00-07:59' as hour, count(date) as people from table1
where '07' between substr(start,1,2) and substr(end,1,2) union
select '08:00-08:59' as hour, count(date) as people from table1
where '08' between substr(start,1,2) and substr(end,1,2) union
select '09:00-09:59' as hour, count(date) as people from table1
where '09' between substr(start,1,2) and substr(end,1,2) union
select '10:00-10:59' as hour, count(date) as people from table1
where '10' between substr(start,1,2) and substr(end,1,2) union
select '11:00-11:59' as hour, count(date) as people from table1
where '11' between substr(start,1,2) and substr(end,1,2) union
select '12:00-12:59' as hour, count(date) as people from table1
where '12' between substr(start,1,2) and substr(end,1,2) union
select '13:00-13:59' as hour, count(date) as people from table1
where '13' between substr(start,1,2) and substr(end,1,2) union
select '14:00-14:59' as hour, count(date) as people from table1
where '14' between substr(start,1,2) and substr(end,1,2) union
select '15:00-15:59' as hour, count(date) as people from table1
where '15' between substr(start,1,2) and substr(end,1,2) union
select '16:00-16:59' as hour, count(date) as people from table1
where '16' between substr(start,1,2) and substr(end,1,2) union
select '17:00-17:59' as hour, count(date) as people from table1
where '17' between substr(start,1,2) and substr(end,1,2) union
select '18:00-18:59' as hour, count(date) as people from table1
where '18' between substr(start,1,2) and substr(end,1,2) union
select '19:00-19:59' as hour, count(date) as people from table1
where '19' between substr(start,1,2) and substr(end,1,2) union
select '20:00-20:59' as hour, count(date) as people from table1
where '20' between substr(start,1,2) and substr(end,1,2) union
select '21:00-21:59' as hour, count(date) as people from table1
where '21' between substr(start,1,2) and substr(end,1,2) union
select '22:00-22:59' as hour, count(date) as people from table1
where '22' between substr(start,1,2) and substr(end,1,2) union
select '23:00-23:59' as hour, count(date) as people from table1
where '23' between substr(start,1,2) and substr(end,1,2)
dbfiddle here (MySQL 8.0.27)
getting concise w col1, would go with:
select substr(start, 1, 2) as hour
, count(start) as people
from table1
group by 1
order by 1
with complete hour notation, as the example:
select concat(substr(start, 1, 2), ':00-', substr(start, 1, 2), ':59') as hour
, count(start) as people
from table1
group by 1
order by 1
dbfiddle here (MySQL 8.0.27)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.