5

给出以下 2 个表:

T1
------------------
From | To  | Value
------------------
10   | 20  | XXX
20   | 30  | YYY
30   | 40  | ZZZ


T2
------------------
From | To  | Value
------------------
10   | 15  | AAA
15   | 19  | BBB
19   | 39  | CCC
39   | 40  | DDD

在 SQL Server 2008 上使用 T-SQL 获得以下结果的最佳方法是什么?

From/To 范围是连续的(没有间隙),下一个From始终与前一个To具有相同的值

Desired result
-------------------------------
From | To  | Value1 |  Value2
-------------------------------
10   | 15  | XXX    |  AAA
15   | 19  | XXX    |  BBB
19   | 20  | XXX    |  CCC
20   | 30  | YYY    |  CCC
30   | 39  | ZZZ    |  CCC
39   | 40  | ZZZ    |  DDD
4

4 回答 4

4

首先,我声明看起来像您发布的数据的数据。如果我所做的任何假设是错误的,请纠正我。最好在问题中发布您自己的声明,以便我们都使用相同的数据。

DECLARE @T1 TABLE (
  [From] INT,
  [To] INT,
  [Value] CHAR(3)
);

INSERT INTO @T1 (
  [From],
  [To],
  [Value]
)
VALUES
  (10, 20, 'XXX'),
  (20, 30, 'YYY'),
  (30, 40, 'ZZZ');

DECLARE @T2 TABLE (
  [From] INT,
  [To] INT,
  [Value] CHAR(3)
);

INSERT INTO @T2 (
  [From],
  [To],
  [Value]
)
VALUES
  (10, 15, 'AAA'),
  (15, 19, 'BBB'),
  (19, 39, 'CCC'),
  (39, 40, 'DDD');

这是我的选择查询以生成您的预期结果:

SELECT
  CASE
    WHEN [@T1].[From] > [@T2].[From]
    THEN [@T1].[From]
    ELSE [@T2].[From]
  END AS [From],
  CASE
    WHEN [@T1].[To] < [@T2].[To]
    THEN [@T1].[To]
    ELSE [@T2].[To]
  END AS [To],
  [@T1].[Value],
  [@T2].[Value]
FROM @T1
INNER JOIN @T2 ON
  (
    [@T1].[From] <= [@T2].[From] AND
    [@T1].[To] > [@T2].[From]
  ) OR
  (
    [@T2].[From] <= [@T1].[From] AND
    [@T2].[To] > [@T1].[From]
  );
于 2012-04-06T18:21:59.443 回答
3

窃取@isme 的数据设置,我写了以下内容:

;With EPs as (
    select [From] as EP from @T1
    union
    select [To] from @T1
    union
    select [From] from @T2
    union
    select [To] from @T2
), OrderedEndpoints as (
    select EP,ROW_NUMBER() OVER (ORDER BY EP) as rn from EPs
)
select
    oe1.EP,
    oe2.EP,
    t1.Value,
    t2.Value
from
    OrderedEndpoints oe1
        inner join
    OrderedEndpoints oe2
        on
            oe1.rn = oe2.rn - 1
        inner join
    @T1 t1
        on
            oe1.EP < t1.[To] and
            oe2.EP > t1.[From]
        inner join
    @T2 t2
        on
            oe1.EP < t2.[To] and
            oe2.EP > t2.[From]

也就是说,您创建一个包含所有可能的句点结束点 ( EPs) 的集合,然后对它们进行“排序”并为每个端点分配一个行号 ( OrderedEPs)。

然后最终查询将每对“相邻”行组合在一起,并连接回原始表以查找每个表中的哪些行与所选范围重叠。

于 2012-04-08T07:45:58.670 回答
0

下面的查询找到最小的范围,然后再次从表中选择值:

SELECT ranges.from, ranges.to, T1.Value, T2.Value
FROM (SELECT all_from.from, min(all_to.to) as to
    FROM (SELECT T1.FROM
        FROM T1
        UNION 
        SELECT T2.FROM 
        FROM T2) all_from
    JOIN (SELECT T1.TO
        FROM T1
        UNION
        SELECT T2.FROM
        FROM T2) all_to ON all_from.from < all_to.to
    GROUP BY all_from.from) ranges
JOIN T1 ON ranges.from >= T1.from AND ranges.to <= T1.to
JOIN T2 ON ranges.from >= T2.from AND ranges.to <= T2.to
ORDER BY ranges.from
于 2012-04-06T18:01:43.553 回答
0

感谢您的回答,但我最终使用了 CTE,我认为它更干净。

DECLARE @T1 TABLE ([From] INT, [To] INT, [Value] CHAR(3));
DECLARE @T2 TABLE ([From] INT, [To] INT, [Value] CHAR(3));

INSERT INTO @T1 (  [From],  [To],  [Value]) VALUES  (10, 20, 'XXX'),  (20, 30, 'YYY'),  (30, 40, 'ZZZ');
INSERT INTO @T2 (  [From],  [To],  [Value]) VALUES  (10, 15, 'AAA'),  (15, 19, 'BBB'),  (19, 39, 'CCC'),  (39, 40, 'DDD');

;with merged1 as 
(
    select 
        t1.[From] as from1,
        t1.[to] as to1,
        t1.Value as Value1,
        t2.[From] as from2,
        t2.[to] as to2,
        t2.Value as Value2
    from @t1 t1
    inner join @T2 t2 
        on t1.[From] <  t2.[To]
        and t1.[To] >= t2.[From]

)
,merged2 as 
(
    select 
          case when from2>=from1 then from2 else from1 end as [From]
         ,case when to2<=to1 then to2 else to1 end as [To]
         ,value1
         ,value2
    from merged1
)
select * from merged2
于 2012-04-06T20:26:44.183 回答