Opusel
@Opusel
Программист

Имея дату создания и закрытия задачи, как определить сколько было активных в день?

В таблица tasks есть 4 записи:
id, date_start,            date_end,             text
1, '2016-01-01 11:00:00', '2016-03-01 11:52:00', 'task 1'
2, '2016-01-03 11:00:00', '2016-01-05 11:52:00', 'task 2'
3, '2016-01-04 11:00:00', '2016-01-07 11:52:00', 'task 3'
4, '2016-01-10 11:00:00', '2016-01-15 11:52:00', 'task 4'


Визуализирую для облегчения восприятия :)
[task 1]=========================================================[/task 1]
              [task 2]========[/task 2]
                    [task 3]===========[/task 3]
                                   [task 4]========[/task 4]


Нужно получить количество активных задач по дню:
day, count
2016-01-01, 1
2016-01-02, 1
2016-01-03, 2
2016-01-04, 3
2016-01-05, 3
2016-01-06, 2
2016-01-07, 2
2016-01-08, 1
2016-01-09, 1
2016-01-10, 2
....
2016-03-01, 1
2016-03-02, 0


Есть ли какой то красивый способ решения в MySQL?
  • Вопрос задан
  • 215 просмотров
Решения вопроса 1
petermzg
@petermzg
Самый лучший программист
select selected_date, (select count(1) from tasks where selected_date between date_start and  date_end) cnt  from 
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date between (select min(date_start) from tasks) and (select max(date_end) from tasks)
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
MaxDukov
@MaxDukov
впишусь в проект как SRE/DevOps.
Сделайте временную таблицу с датами, далее LEFT JOIN с ней с условием, что day >=date_start и day =
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы