I have the following table:
| start_date | end_date | sales | is_finished |
|:------------------------:|---------------------:|:------------:|:------------:
| 2017-03-24 09:11:00 | 2017-03-24 09:11:00 | 30 | 1
| 2017-03-24 08:30:00 | 2017-03-24 08:30:00 | 50 | 1
| 2017-03-24 08:14:00 | 2017-03-24 08:14:00 | 100 | 1
| 2017-03-24 07:30:00 | 2017-03-24 07:45:00 | 200 | 0
| 2017-03-24 07:00:00 | 2017-03-24 07:00:00 | 150 | 1
| 2017-03-24 06:40:00 | 2017-03-24 06:50:00 | 450 | 0
I need to group by start_date
and end_date
and add the value of the column sales
. Now, they must be grouped in the same way each time the column is_finished
has a one.
That is, columns that have a zero will be added and when a one appears it should be grouped as a set.
I need the following result:
| start_date | end_date | SUM(sales) |
|:------------------------:|---------------------:|:------------:|
| 2017-03-24 09:11:00 | 2017-03-24 09:11:00 | 30 |
| 2017-03-24 08:30:00 | 2017-03-24 08:30:00 | 50 |
| 2017-03-24 07:30:00 | 2017-03-24 08:14:00 | 300 |
| 2017-03-24 06:40:00 | 2017-03-24 07:00:00 | 600 |
For now I have a query but the problem is that when there are 2 or more rows that have the value 1 in the column is_finished
take them as a group.
The query is:
SELECT SUM(sales) , MIN(start_date) , MAX(end_date)
FROM sales
GROUP BY
start_date > (SELECT start_date FROM sales WHERE is_finished = 1 ORDER BY end_date ASC LIMIT 1 OFFSET 1 ),
end_date <= (SELECT end_date FROM sales WHERE is_finished = 1 ORDER BY end_date ASC LIMIT 1)
ORDER BY end_date DESC
Using the query above I approach what I expect, but it has the problem mentioned.