V
V
Vladimir V.2016-12-16 14:42:40
MySQL
Vladimir V., 2016-12-16 14:42:40

Given the date of creation and closing of the task, how to determine how many were active per day?

There are 4 entries in the tasks table:

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'

I visualize to make it easier to understand :)
[task 1]=========================================================[/task 1]
              [task 2]========[/task 2]
                    [task 3]===========[/task 3]
                                   [task 4]========[/task 4]

You need to get the number of active tasks per day:
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

Is there a pretty way to do this in MySQL?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Peter, 2016-12-16
@Opusel

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)

M
Max, 2016-12-16
@MaxDukov

Make a temporary table with dates, then LEFT JOIN with it with the condition that day >=date_start and day =

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question