Given the sample data:
CREATE TABLE table_name (ID, "DESC", FromDate, ToDate, Colour) AS
--SELECT 'ID_01', 'A', DATE '2017-08-01', DATE '2020-10-01', 'Red' FROM DUAL UNION ALL
--SELECT 'ID_02', 'B', DATE '2019-02-01', DATE '2029-09-01', 'Blue' FROM DUAL UNION ALL
--SELECT 'ID_03', 'C', DATE '2014-02-01', DATE '2019-02-01', 'Black' FROM DUAL UNION ALL
SELECT 'ID_04', 'D', DATE '2010-04-01', DATE '2019-01-01', 'Yellow' FROM DUAL UNION ALL
SELECT 'ID_05', 'D', DATE '2019-01-01', DATE '2019-09-01', 'Green' FROM DUAL;
(and just focusing on the D values for DESC)
Then the accepted answer to the linked question:
WITH calendar (month) AS (
SELECT ADD_MONTHS(DATE '2019-01-01', LEVEL - 1)
FROM DUAL
CONNECT BY ADD_MONTHS(DATE '2019-01-01', LEVEL - 1) <= DATE '2019-09-01'
)
SELECT month,
MIN(id) AS id,
"DESC"
FROM calendar c
INNER JOIN table_name t
ON (c.month BETWEEN t.fromdate and t.todate)
GROUP BY month, "DESC"
ORDER BY month, id;
Only outputs a single DESC values for each day and there is only the corresponding minimum ID:
| MONTH |
ID |
DESC |
| 2019-01-01 00:00:00 |
ID_04 |
D |
| 2019-02-01 00:00:00 |
ID_05 |
D |
| 2019-03-01 00:00:00 |
ID_05 |
D |
| 2019-04-01 00:00:00 |
ID_05 |
D |
| 2019-05-01 00:00:00 |
ID_05 |
D |
| 2019-06-01 00:00:00 |
ID_05 |
D |
| 2019-07-01 00:00:00 |
ID_05 |
D |
| 2019-08-01 00:00:00 |
ID_05 |
D |
| 2019-09-01 00:00:00 |
ID_05 |
D |
The reason that you get ID_04 for 2019-01-01 and then ID_05 for the rest of the days is that ID_04 has a toDate value of 2019-01-01 so it cannot be included in the range from 2019-02-01 through to 2019-09-01 as those months are outside the upper bound of its range. Instead, you get ID_05 for those months because they are within its range.
fiddle