使用递归 cte 进行 sql 连接

Exc*_*arn 5 sql sql-server sql-server-2008

编辑:在注释中添加了另一个案例场景并更新了示例附件。

我正在尝试编写一个 sql 来获取附带此问题的输出以及示例数据。有两张表,一张具有不同的 ID (pk) 及其当前标志。另一个具有活动ID(从第一个表中fk到pk)和非活动ID(从第一个表中fk到pk)最终输出应返回两列,第一列由第一个表中的所有不同ID组成,第二列应包含第二个表中的活动 ID。下面是sql:

IF OBJECT_ID('tempdb..#main') IS NOT NULL DROP TABLE #main;
IF OBJECT_ID('tempdb..#merges') IS NOT NULL DROP TABLE #merges
IF OBJECT_ID('tempdb..#final') IS NOT NULL DROP TABLE #final

SELECT DISTINCT id, 
       current
INTO   #main 
FROM   tb_ID t1 


--get list of all active_id and inactive_id 

SELECT DISTINCT active_id, 
            inactive_id, 
            Update_dt  
INTO   #merges 
FROM   tb_merges 
-- Combine where the id from the main table matched to the inactive_id (should return all the rows from #main)

   SELECT id, 
   active_id AS merged_to_id 
   INTO   #final 
   FROM   (SELECT t1.*, 
           t2.active_id, 
           Update_dt , 
           Row_number() 
             OVER ( 
               partition BY id, active_id 
               ORDER BY Update_dt DESC) AS rn 
    FROM   #main t1 
           LEFT JOIN #merges t2 
                  ON t1.id = t2.inactive_id) t3 
  WHERE  rn = 1 

 SELECT *
 FROM #final
Run Code Online (Sandbox Code Playgroud)

这个sql部分有效。它不起作用,id 曾经处于活动状态,然后变得不活动。请注意:

  • 活动 ID 应返回最后一个最活动的 ID
  • 没有任何活动 ID 的 ID 应该为 null 或 ID 本身
  • 当前 = 0 的 ID,在这种情况下,活动 ID 应为 tb_ID 中的当前 ID

  • ID 可能会互换。例如,有两个 ID 6 和 7,当 6 处于活动状态时,7 处于非活动状态,反之亦然。了解最新活动状态的唯一方法是通过更新日期

附上示例可能很容易理解

具有预期输出的样本数据

看起来我可能必须使用递归 cte 才能获得结果。有人可以帮忙吗?感谢您的时间!

Joe*_*ell 1

我认为你是正确的,递归 CTE 看起来是一个很好的解决方案。我不完全确定我已经完全理解您的要求,特别是关于该update_dt专栏,只是因为数据按原样有点抽象,但我已经尝试过,并且它似乎确实适用于您的示例数据。评论解释了发生了什么。

declare @tb_id table (id bigint, [current] bit);
declare @tb_merges table (active_id bigint, inactive_id bigint, update_dt datetime2);
insert @tb_id values
    -- Sample data from the question.
    (1, 1),
    (2, 1),
    (3, 1),
    (4, 1),
    (5, 0),
    -- A few additional data to illustrate a deeper search.
    (6, 1),
    (7, 1),
    (8, 1),
    (9, 1),
    (10, 1);

insert @tb_merges values
    -- Sample data from the question.
    (3, 1, '2017-01-11T13:09:00'),
    (1, 2, '2017-01-11T13:07:00'),
    (5, 4, '2013-12-31T14:37:00'),
    (4, 5, '2013-01-18T15:43:00'),
    -- A few additional data to illustrate a deeper search.
    (6, 7, getdate()),
    (7, 8, getdate()),
    (8, 9, getdate()),
    (9, 10, getdate());

if object_id('tempdb..#ValidMerge') is not null
    drop table #ValidMerge;

-- Get the subset of merge records whose active_id identifies a "current" id and
-- rank by date so we can consider only the latest merge record for each active_id.
with ValidMergeCTE as
(
    select
        M.active_id,
        M.inactive_id,
        [Priority] = row_number() over (partition by M.active_id order by M.update_dt desc)
    from 
        @tb_merges M
        inner join @tb_id I on M.active_id = I.id 
    where
        I.[current] = 1
)
select
    active_id,
    inactive_id
into
    #ValidMerge
from
    ValidMergeCTE
where
    [Priority] = 1;

 -- Here's the recursive CTE, which draws on the subset of merges identified above.
 with SearchCTE as
 (
    -- Base case: any record whose active_id is not used as an inactive_id is an endpoint.
    select
        M.active_id,
        M.inactive_id,
        Depth = 0
    from
        #ValidMerge M
    where
        not exists (select 1 from #ValidMerge M2 where M.active_id = M2.inactive_id)

    -- Recursive case: look for records whose active_id matches the inactive_id of a previously
    --                 identified record.
    union all
    select
        S.active_id,
        M.inactive_id,
        Depth = S.Depth + 1
    from
        #ValidMerge M
        inner join SearchCTE S on M.active_id = S.inactive_id
 )
 select
    I.id,
    S.active_id
 from
    @tb_id I
    left join SearchCTE S on I.id = S.inactive_id;
Run Code Online (Sandbox Code Playgroud)

结果:

id      active_id
------------------
1       3
2       3
3       NULL
4       NULL
5       4
6       NULL
7       6
8       6
9       6
10      6
Run Code Online (Sandbox Code Playgroud)