确定范围是否完全被一组范​​围覆盖

时间:2018-09-18 10:00:04

标签: sql sql-server date range gaps-and-islands

如何检查某个范围是否被一组范围 完全覆盖 。在以下示例中:

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 -- ?
  • 我要选择10, 60,因为所有内容都被0, 4040, 60(以及10, 30)覆盖
  • 我想排除10, 90,因为它暴露在60, 80之间

假定a是包含性的,而b是排除性的,即值40属于[40, 60)而不是[0, 40)。范围可以包含间隙和各种重叠。

实际问题涉及日期和时间数据,但日期只是数字。我正在使用SQL Server,但首选通用解决方案。

4 个答案:

答案 0 :(得分:2)

这是类似于Thorsten的递归解决方案。只是提供另一个示例。

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 as id, 10 as a, 90 as b
        UNION
        SELECT 2, 10, 60
), rangeFinder(a, b, ra, rfb) AS
(
    SELECT a, b, 0 AS ra, 0 AS rfb 
    FROM ranges AS r
    UNION ALL
    SELECT rangeFinder.a, ranges.b, ranges.a, rangeFinder.b 
    FROM ranges 
    JOIN rangeFinder
        ON ranges.b > rangeFinder.b
        AND ranges.a <=rangeFinder.b
), islands(a, b) AS
(
    SELECT a, b 
    FROM rangeFinder
    WHERE a NOT IN (SELECT ra FROM rangeFinder)
        AND b NOT IN (SELECT rfb FROM rangeFinder)
)
SELECT t.id, t.a, t.b FROM 
tests t
JOIN islands i
ON t.a >= i.a
AND t.b <= i.b

此处演示:http://rextester.com/HDQ52126

答案 1 :(得分:1)

您想要一个递归查询来查找实际范围(在您的情况下为0到60和80到100)。我们将从给出的范围开始,然后寻找扩展这些范围的范围。最后,我们坚持使用最宽的范围(例如,可以将范围10到30扩展到0到40,然后再扩展到0到60,因此我们将最宽的范围保持在0到60)。

with wider_ranges(a, b, grp) as
(
  select a, b, id from ranges
  union all
  select
    case when r1.a < r2.a then r1.a else r2.a end,
    case when r1.b > r2.b then r1.b else r2.b end,
    r1.grp
  from wider_ranges r1
  join ranges r2 on (r2.a < r1.a and r2.b >= r1.a)
                 or (r2.b > r1.b and r2.a <= r1.b)
)
, real_ranges(a, b) as
(
  select distinct min(a), max(b)
  from wider_ranges
  group by grp
)
select * 
from tests
where exists
(
  select *
  from real_ranges
  where tests.a >= real_ranges.a and tests.b <= real_ranges.b
);

上个月的演示:http://rextester.com/BDJA16583

根据要求,它可以在SQL Server中工作,但是它是标准SQL,因此它应该可以在所有具有递归查询的DBMS中使用。

答案 2 :(得分:0)

这是解决方案的一般形式。该想法是要执行以下操作:

  • 获取所有不在范围内的点的列表。这是所有范围的开始和范围的结束。
  • 检查是否有一个不在范围内。

这是基于以下操作:不在范围内的点将成为任一表中包含的数字之一:

with tc as (
      select t.test, r.candidate
      from tests t join
           (select r.a as candidate from ranges union all
            select r.b from ranges
           ) r
           on r.candiate >= t.a and r.candidate < t.b
      union all
      select t.test, t.a
      from tests t
      union all
      select t.test, t.b
      from tests t
     )
select distinct tc.test
from tc
where not exists (select 1
                  from ranges r
                  where tc.candidate >= r.a and tc.candidate < r.b
                 );

由于范围包括第一项,因此您实际上不必进行检查。因此可以减少候选人的名单:

with tc as (
      select t.test, r.candidate
      from tests t join
           (select r.b as candidate from ranges
           ) r
           on r.candidate >= t.a and r.r < t.b
      union all
      select t.test, t.a
      from tests t
      union all
      select t.test, t.b
      from tests t
     )

答案 3 :(得分:0)

如接受的答案中所述,解决方案是将重叠范围合并在一起,然后确定合并范围之一内是否存在测试范围。

除了连接和/或递归,您还可以将sorting approach与窗口函数一起使用来合并重叠范围:

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
), ranges_chg AS (
    SELECT *, CASE WHEN MAX(b) OVER (ORDER BY a ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) >= a THEN 0 ELSE 1 END AS chg
    FROM ranges
), ranges_grp AS(
    SELECT *, SUM(chg) OVER (ORDER BY a) AS grp
    FROM ranges_chg
), merged_ranges AS (
    SELECT MIN(a) AS a, MAX(b) AS b
    FROM ranges_grp
    GROUP BY grp
)
SELECT *
FROM tests
WHERE EXISTS (
    SELECT 1
    FROM merged_ranges
    WHERE merged_ranges.a <= tests.a AND tests.b <= merged_ranges.b
)

结果和Fiddle

| id | a  | b  |
|----|----|----|
| 2  | 10 | 60 |

range_grp CTE中的数据将使您了解其工作方式:

| id | a  | b   | max b over... | chg | grp |
|----|----|-----|---------------|-----|-----|
| 1  | 0  | 40  | NULL          | 1   | 1   |
| 4  | 10 | 30  | 40            | 0   | 1   |
| 2  | 40 | 60  | 40            | 0   | 1   |
| 3  | 80 | 100 | 60            | 1   | 2   |