CTE递归更新字段值,直到没有默认值

时间:2018-07-03 08:18:23

标签: recursion sql-update common-table-expression sql-cte

我正试图编写一个递归来更新字段的值,直到没有默认值'0'为止。

起点是此表:

    USE [Test]

    DROP TABLE IF EXISTS [Test].[dbo].[T0]

    CREATE TABLE [Test].[dbo].[T0] (
    ID1 nvarchar(50),
    ID2 nvarchar(50),
    Score decimal(18,0),
    tag int
    );

    INSERT INTO [Test].[dbo].[T0] (ID1, ID2, Score, tag)
    VALUES ('A1', 'B1', 100.0, 0),
    ('A1', 'B2', 90, 0),
    ('A2', 'B1', 80, 0),
    ('A2', 'B3', 70, 0),
    ('A3', 'B4', 85, 0),
    ('A4', 'B4', 75, 0),
    ('A5', 'B5', 85, 0),
    ('A5', 'B6', 75, 0);

[tag] = 0的所有行的任务是:

  1. 将字段[tag]的值更新为得分最高的行“ 1”

  2. 将字段[tag]的值更新为“ 2”,以使其与具有最高得分的行具有相同的[ID1]或[ID2]。

  

递归应继续进行,直到不再有值'0'的行为止。

所需的输出如下:

ID1 ID2 Score   tag
A1  B1  100 1
A1  B2  90  2
A2  B1  80  2
A2  B3  70  1
A3  B4  85  1
A4  B4  75  2
A5  B5  85  1
A5  B6  75  2

我写了以下CTE,它不会循环。我必须运行四次才能获得正确的输出。

WITH T1 AS
(
    Select distinct [ID1], [ID2], [Score], [tag]
    ,Rank() over (order by [Score] desc, [ID1] desc, [ID2] desc) as [rk]
    from [Test].[dbo].[T0] 
    where [tag] = 0
    group by [ID1], [ID2], [Score], [tag]

)
,T2 AS
(
    Select distinct [ID1], [ID2]
    ,[Score]
    ,[rk]
    ,case when rk = 1 then 1 else [tag] end as [tag]
    from [T1]
)

,T3 AS
(
    Select [ID1], [ID2], [Score], 0 as rk, case when rk <> 1 and ([Top_ID1] is not null or [Top_ID2] is not null) then 2 else [tag] end as [tag]
    from 

        (Select [ID1], [ID2], [Score], rk, tag from [T2]) T3_1
        left outer join
        (Select  [ID1] as [Top_ID1], [ID2] as [Top_ID2] from [T2] where  rk = 1) T3_2
        on T3_1.[ID1] = T3_2.[Top_ID1] or T3_1.[ID2] = T3_2.[Top_ID2]
)
,T4 AS
(
    Select distinct [ID1], [ID2], [Score], [rk], [tag] 
    from [T3]
    where [tag] = 0 

    union all

    Select [ID1], [ID2], [Score], [rk], [tag] 
    from [T4]
    where [tag] = 0 
)
Update [Test].[dbo].[T0]
Set [tag] = [T4].[tag]
from [T4]
join [Test].[dbo].[T0] D on D.[ID1] = [T4].[ID1] and D.[ID2] = [T4].[ID2]
option ( MaxRecursion 0 );
  

有人会知道如何修改代码以使递归起作用吗?

感谢您的帮助!

0 个答案:

没有答案