如何识别考虑范围类型的重叠数范围?

时间:2017-05-18 09:06:58

标签: sql sql-server

我有两张桌子TABLE1(记录列表)&表2(我要检查的数量范围)

如何识别TABLE2中的重叠范围/行(考虑TypeId?

目的

RECORDS TABLE
+-----------------+-----------------+-------------------+
+     typeId      + tbl1_NumberFrom + tbl1_NumberTo     +
+-----------------+-----------------+-------------------+
+       1         +      1          +        5          +
+       1         +      8          +        10         +
+       1         +      15         +        20         +
+       2         +      4          +        7          +
+       2         +      9          +        10         +
+       2         +      11         +        20         +
+-----------------+-----------------+-------------------+

MY RANGES TABLE TO CHECK
+-----------------+-----------------+-------------------+
+      typeId     + My_NumberFrom   + My_NumberTo       +
+-----------------+-----------------+-------------------+
+      1          +      1          +        3          +
+      1          +      6          +        8          +
+      1          +      11         +        12         +
+      2          +      1          +        3          +
+      2          +      6          +        8          +
+      2          +      10         +        10         +
+-----------------+-----------------+-------------------+

REQUIRED RESULT AS FOLLOW:
+-----------------+-----------------+-------------------+-------------------+
+      typeId     + My_NumberFrom   + My_NumberTo       +   IsOverlapping   +
+-----------------+-----------------+-------------------+-------------------+
+      1          +      1          +        3          +        YES        +
+      2          +      1          +        3          +        NO         +
+      1          +      6          +        8          +        YES        +
+      2          +      6          +        8          +        YES        +
+      1          +      11         +        12         +        NO         +
+      2          +      10         +        10         +        YES        +
+-----------------+-----------------+-------------------+-------------------+

更多,

上表中的SQL

IF OBJECT_ID('tempdb.dbo.#dbtable', 'U') IS NOT NULL  DROP TABLE #dbtable; 
SELECT * INTO #dbtable FROM 
    (SELECT 1 typeId, 1 AS tbl1_NumberFrom, 5 AS tbl1_NumberTo
    UNION ALL 
    SELECT 1 typeId, 8 AS tbl1_NumberFrom, 10 AS tbl1_NumberTo
    UNION ALL 
    SELECT 1 typeId, 15 AS tbl1_NumberFrom, 20 AS tbl1_NumberTo 
    UNION ALL 
    SELECT 2 typeId, 4  AS tbl1_NumberFrom, 7 AS tbl1_NumberTo 
    UNION ALL 
    SELECT 2 typeId, 9 AS tbl1_NumberFrom, 10 AS tbl1_NumberTo 
    UNION ALL 
    SELECT 2 typeId, 11 AS tbl1_NumberFrom, 20 AS tbl1_NumberTo) 
AS dbTable

IF OBJECT_ID('tempdb.dbo.#MyRanges', 'U') IS NOT NULL  DROP TABLE #MyRanges; 
SELECT * INTO #MyRanges FROM 
    (SELECT 1 typeId, 1 AS myr_NumberFrom, 3 AS myr_NumberTo
    UNION ALL 
    SELECT 1 typeId, 6 AS myr_NumberFrom, 8 AS myr_NumberTo 
    UNION ALL 
    SELECT 1 typeId, 11 AS myr_NumberFrom, 12 AS myr_NumberTo
    UNION ALL
    SELECT 2 typeId, 1 AS My_NumberFrom, 3 AS myr_NumberTo
    UNION ALL 
    SELECT 2 typeId, 6 AS myr_NumberFrom, 8 AS myr_NumberTo 
    UNION ALL 
    SELECT 2 typeId, 10 AS myr_NumberFrom, 10 AS myr_NumberTo)
AS MyList


SELECT * FROM #dbtable t
SELECT * FROM #MyRanges m

请建议,

非常感谢!!

3 个答案:

答案 0 :(得分:1)

$upd_qry = "update tbl_quotes 
    set qu_status='".$_GET['status']."', qu_time=NOW()              
    where id='".$_GET['quotes_id']."'";

答案 1 :(得分:1)

检查两个范围是否重叠的方法是测试一个是否在另一个结束之前开始,而另一个在第一个结束之前开始。
这是在t-sql中执行此操作的一种方法:

vertical-align: baseline;

结果:

SELECT  typeId, 
        myr_NumberFrom, 
        myr_NumberTo,
        CASE WHEN EXISTS
        (
            SELECT 1
            FROM #dbtable t
            WHERE t.typeId = m.typeId
            AND tbl1_NumberFrom <= myr_NumberTo
            AND tbl1_NumberTo >= myr_NumberFrom
        ) THEN 'Yes'
        ELSE 'No'
        END As IsOverlapping   
FROM #MyRanges m

更改为左连接而不是子查询:

typeId  myr_NumberFrom  myr_NumberTo    IsOverlap
1       1               3               Yes
1       6               8               Yes
1       11              12              No
2       1               3               No
2       6               8               Yes
2       10              10              Yes

结果相同,现场演示已更新。

You can see a live demo on rextester.

答案 2 :(得分:1)

我的答案几乎与你的另一个问题的答案相同。

有四种类型的重叠。您需要两个条件才能找到重叠。其他答案是忘记重叠类型,其中myr_numberfrom&lt; tbl1.numberfrom和myr_numberto&gt; tbl1_numberto。

Select distinct m.typeId, m.Myr_NumberFrom,m.Myr_NumberTo, 
        case when t.tbl1_NumberFrom is null then 'No' else 'yes' end isOverlapping
from #MyRanges m
left join #dbtable t on m.typeId = t.typeId
                and (m.Myr_NumberFrom between t.tbl1_NumberFrom and t.tbl1_NumberTo
                or  t.tbl1_NumberFrom between m.Myr_NumberFrom and m.Myr_Numberto)