I can not imagine how to fix my problem using t-sql.
- I need MIN(ValidFrom) and MAX(ValidTo) out of a table with different time blocks considering a chronological sequence and a number.
- A normal group won't work because MIN(ValidFrom) and MAX(ValidTo) for num 100 will not be correct.
- I can not imagine how to get the expected results from this table using any analytic or rank function in t-sql.
I need minimum and maximum dates out of the following table.
| ValidFrom | ValidTo | Num | ID Number |
|---|---|---|---|
| 2019-06-01 | 2019-12-31 | 100 | 01234567 |
| 2020-01-01 | 2020-03-31 | 100 | 01234567 |
| 2020-04-01 | 2020-12-31 | 100 | 01234567 |
| 2021-01-01 | 2021-01-31 | 100 | 01234567 |
| 2021-02-01 | 2021-03-31 | 50 | 01234567 |
| 2021-04-01 | 2021-09-30 | 50 | 01234567 |
| 2021-10-01 | 2021-12-31 | 50 | 01234567 |
| 2022-01-01 | 2022-04-30 | 100 | 01234567 |
Expected result of the query:
| ValidFrom | ValidTo | Num | ID Number |
|---|---|---|---|
| 2019-06-01 | 2021-01-31 | 100 | 01234567 |
| 2021-02-01 | 2021-12-31 | 50 | 01234567 |
| 2022-01-01 | 2022-04-30 | 100 | 01234567 |