How can I check if a range is completely covered by a set of ranges. In the following example:
WITH ranges(id, a, b) AS (
SELECT 1, 0, 40 UNION
SELECT 2, 40, 60 UNION
SELECT 3, 80, 100 UNION
SELECT 4, 10, 30
), tests(id, a, b) AS (
SELECT 1, 10, 90 UNION
SELECT 2, 10, 60
)
SELECT *
FROM tests
WHERE -- ?
- I want to select
10, 60because all of it is covered by0, 40and40, 60(and10, 30) - I want to exclude
10, 90because it is exposed between60, 80
Assume that a is inclusive and b is exclusive i.e. the value 40 belongs to [40, 60) and not [0, 40). The ranges can contain gaps and all kind of overlaps.
The actual problem involves date+time data but dates are just numbers. I am using SQL server but generic solution is preferred.