I have a table where I registry when an event start and when ends. I want a list of the events that ends between my date range or are in progress yet, but those that hasn't ended yet are wrong because the null value is missed in the group concat.
How can I get the null values with grouping by entity_id?
| id | entity_id | start | end |
|----|-----------|----------------------|----------------------|
| 1 | 10 | 2020-05-22T23:50:00Z | 2020-05-23T00:15:00Z |
| 2 | 20 | 2020-05-22T23:30:00Z | 2020-05-23T00:50:00Z |
| 3 | 10 | 2020-05-23T01:00:00Z | 2020-05-23T01:10:00Z |
| 4 | 20 | 2020-05-23T01:30:00Z | 2020-05-23T01:50:00Z |
| 5 | 20 | 2020-05-23T02:00:00Z | null |
| 6 | 10 | 2020-05-23T05:00:00Z | null |
My query:
SELECT entity_id,
SUBSTRING_INDEX(GROUP_CONCAT(`start` ORDER BY `start` DESC SEPARATOR '||'), '||', 1) AS `start`,
SUBSTRING_INDEX(GROUP_CONCAT(`end` ORDER BY `start` DESC SEPARATOR '||'), '||', 1) AS `end`
FROM my_table
WHERE `start` <= '2020-05-23 23:59:59'
AND ( `end` IS NULL OR `end` BETWEEN '2020-05-23 00:00:00' AND '2020-05-23 23:59:59' )
GROUP BY entity_id ;
This is the query result
| entity_id | start | end |
|-----------|---------------------|---------------------|
| 10 | 2020-05-23 05:00:00 | 2020-05-23 01:10:00 |
| 20 | 2020-05-23 02:00:00 | 2020-05-23 01:50:00 |
But this is what should be
| entity_id | start | end |
|-----------|---------------------|---------------------|
| 10 | 2020-05-23 05:00:00 | null |
| 20 | 2020-05-23 02:00:00 | null |
I created an example: http://sqlfiddle.com/#!9/e04be2/1/0