I have a dataframe with the schema (ID, start_date, end_date)
Sample dataframe:
| id | start_date | end_date |
|---|---|---|
| 1 | 03/05/2020 | 07/05/2020 |
| 1 | 05/05/2020 | 07/05/2020 |
| 1 | 01/05/2020 | 07/05/2020 |
| 1 | 01/05/2020 | 13/05/2020 |
| 2 | 06/05/2020 | 09/05/2020 |
| 1 | 18/05/2020 | 20/05/2020 |
| 2 | 09/05/2020 | 17/05/2020 |
I want the result in below format
| id | start_dates | days |
|---|---|---|
| 1 | [03/05/2020, 05/05/2020, 01/05/2020, 01/05/2020, 18/05/2020] | 16 |
| 2 | [06/05/2020, 09/05/2020] | 12 |
days is the sum of non overlapping date intervals (start_date - end_date).
The approach that i tried is to group by id, collect start_date and end_date as lists and then write a UDF to get the days column. I would like to know if there is a better way to achieve this.