CTE 递归 select 并使用新 ID 和一些更新的字段插入相同的 table
CTE recursive select and insert in same table with new id's and some updated fields
假设我有这个 table:
|TeamId |Name |ActiveYear |ParentTeamId |OriginTeamId |
+-------+---------------+-----------+-------------+-------------+
|1 |Founding Team |2020 |NULL |NULL |
|2 |Elders Team |2020 |1 |NULL |
|3 |Younger Team |2020 |1 |NULL |
|4 |Women Team |2020 |2 |NULL |
|5 |Men´s Team |2020 |2 |NULL |
查询的时候可以看到层级:
WITH abcd AS
(
-- anchor
SELECT
CAST((Convert(varchar(20), TeamId)) AS VARCHAR(1000)) AS "Tree",
TeamId, [Name], Activeyear, ParentTeamId, OriginTeamId
FROM
Teams AS p
WHERE
ParentTeamId IS NULL And ActiveYear = 2020
UNION ALL
--recursive member
SELECT
CAST((CONVERT(varchar(20), a.tree) + '/' + CONVERT(varchar(20), c.TeamId)) AS VARCHAR(1000)) AS "Tree",
c.TeamId, c.[Name], c.Activeyear, c.ParentTeamId, c.OriginTeamId
FROM
Teams AS c
JOIN
abcd AS a ON c.ParentTeamId = a.TeamId
WHERE
c.ActiveYear = 2020
)
SELECT *
FROM abcd
现在,我需要更新此 table,重新创建相同的结构,但使用新 ID 和 Activeyear = 2021。
因此,新的父记录仍将是其他新记录的父记录。
我想查询,这允许我“克隆”此数据并将其插入一些更新:ActiveYear = 2021,OriginTeamId(OriginTeamId 用于识别该信息的来源)
我该怎么做?
一种方法是分两步完成。插入没有 ID 的行。然后算出ids:
insert into teams (name, activeyear, parentid)
select name, 2021, parentid
from teams
where actdiveyear = 2020;
现在通过查找相应的 ID 来更新值。 Window 函数可以提供帮助:
insert into teams (name, activeyear, ParentTeamId)
select name, 2021, ParentTeamId
from teams
where activeyear = 2020;
with corresponding as (
select t2020.teamid as teamid_2020, t2021.teamid as teamid_2021
from teams t2020 join
teams t2021
on t2020.name = t2021.name
where t2020.activeyear = 2020 and t2021.activeyear = 2021
)
update t
set ParentTeamId = c.teamid_2021
from teams t join
corresponding c
on t.ParentTeamId = c.teamid_2020
where t.activeyear = 2021;
Here 是一个 db<>fiddle.
假设我有这个 table:
|TeamId |Name |ActiveYear |ParentTeamId |OriginTeamId |
+-------+---------------+-----------+-------------+-------------+
|1 |Founding Team |2020 |NULL |NULL |
|2 |Elders Team |2020 |1 |NULL |
|3 |Younger Team |2020 |1 |NULL |
|4 |Women Team |2020 |2 |NULL |
|5 |Men´s Team |2020 |2 |NULL |
查询的时候可以看到层级:
WITH abcd AS
(
-- anchor
SELECT
CAST((Convert(varchar(20), TeamId)) AS VARCHAR(1000)) AS "Tree",
TeamId, [Name], Activeyear, ParentTeamId, OriginTeamId
FROM
Teams AS p
WHERE
ParentTeamId IS NULL And ActiveYear = 2020
UNION ALL
--recursive member
SELECT
CAST((CONVERT(varchar(20), a.tree) + '/' + CONVERT(varchar(20), c.TeamId)) AS VARCHAR(1000)) AS "Tree",
c.TeamId, c.[Name], c.Activeyear, c.ParentTeamId, c.OriginTeamId
FROM
Teams AS c
JOIN
abcd AS a ON c.ParentTeamId = a.TeamId
WHERE
c.ActiveYear = 2020
)
SELECT *
FROM abcd
现在,我需要更新此 table,重新创建相同的结构,但使用新 ID 和 Activeyear = 2021。
因此,新的父记录仍将是其他新记录的父记录。
我想查询,这允许我“克隆”此数据并将其插入一些更新:ActiveYear = 2021,OriginTeamId(OriginTeamId 用于识别该信息的来源)
我该怎么做?
一种方法是分两步完成。插入没有 ID 的行。然后算出ids:
insert into teams (name, activeyear, parentid)
select name, 2021, parentid
from teams
where actdiveyear = 2020;
现在通过查找相应的 ID 来更新值。 Window 函数可以提供帮助:
insert into teams (name, activeyear, ParentTeamId)
select name, 2021, ParentTeamId
from teams
where activeyear = 2020;
with corresponding as (
select t2020.teamid as teamid_2020, t2021.teamid as teamid_2021
from teams t2020 join
teams t2021
on t2020.name = t2021.name
where t2020.activeyear = 2020 and t2021.activeyear = 2021
)
update t
set ParentTeamId = c.teamid_2021
from teams t join
corresponding c
on t.ParentTeamId = c.teamid_2020
where t.activeyear = 2021;
Here 是一个 db<>fiddle.