I have a table where information about the execution-interval of tasks is stored. Every Task has a starting year and the interval for the execution. E.g. Start 2020, interval = 2 years would mean the task will be executed in 2020,2022,2024 and so on.
How can I check if there are overlapping intervals in the table?
ID | StartYear | Interval
____|_____________|___________
1 | 2020 | 3
2 | 2019 | 4
3 | 2020 | 5
4 | 2019 | 2
5 | 2020 | 2
Result: Task 1 and two overlap (first time in 2023), Task 2 and 3 overlap (first time in2035), task 1 and 3 overlap (first time in 2020, then 2035), task 4 and 5 do not overlap each other (one is always on even years, the other on uneven years) and so on.
So mathematically, I want to know how to check if two infinite sets are disjoint. My problem is, I don't know how to even represent an infinite set by the data I have in MS-SQL.
The endresult would be a query (or function) that you can give a starting year and an interval as a parameter that returns all overlapping entries.
Example 1
Given a starting year of 2020 and an interval of 2, the set to compare against would be 2020,2022,2024,2026 etc. That's why in the result ID 2 and 4 are not selected, because they both start at an uneven year and the interval is an even number, meaning the execution year will always be an uneven year, which is not in the comparison set of 2020,2022,2024 etc.
Declare @CompareStart int = 2020;
Declare @CompareInterval int = 2;
SELECT * FROM MYTABLE WHERE OVERLAPPING(@CompareStart, @CompareInterval)
Result would be:
ID | StartYear | Interval
____|_____________|___________
1 | 2020 | 3
3 | 2020 | 5
5 | 2020 | 2
Example 2
For a starting year of 2019 and an interval of 3, the resultset would be the rows with IDs 2 (first hit 2019), 3 (first hit 2025), 4 (first hit 2019) and 5 (first hit 2022).
Declare @CompareStart int = 2019;
Declare @CompareInterval int = 3;
SELECT * FROM MYTABLE WHERE OVERLAPPING(@CompareStart, @CompareInterval)
Result would be:
ID | StartYear | Interval
____|_____________|___________
2 | 2019 | 4
3 | 2020 | 5
4 | 2019 | 2
5 | 2020 | 2