递归 CTE SQL (Parent Child)
Recursive CTE SQL (Parent Child)
我正在使用 SQL Server 2012
位置详情table
OID LOCATIONNAME PARENTID
1 GLOBAL 0
2 NORTH 1
3 SOUTH 1
4 NORTH1 2
5 SOUTH1 3
位置站点配置table
OID LOCATIONID SITENAME
1 2 TEST
我使用递归 CTE 查询
;WITH LOCALSITEHIERARCHY AS
(
SELECT A.OID
,A.PARENTOID
,CAST(A.LOCATIONNAME + ' ( ' + LSC.SITENAME + ' )' AS NVARCHAR(100)) AS NAME
,LSC.OID AS SITEOID
FROM LOCATIONDETAIL A
INNER JOIN LOCATIONSITECONFIG LSC
ON LSC.LOCATIONDETAILOID = A.OID
WHERE
LSC.SITENAME <> 'GLOBAL' AND LSC.RECSTATUS = 'A'
UNION ALL
SELECT
A.OID
,A.PARENTOID
,CAST(A.LOCATIONNAME AS NVARCHAR(100))
,LH.SITEOID
FROM LOCATIONDETAIL A
INNER JOIN LOCALSITEHIERARCHY LH ON A.PARENTOID = LH.OID
)
SELECT * FROM LOCALSITEHIERARCHY
NORTH 现在是 Global.This 中的一个单独站点 查询 returns North 和 North 1 在树结构中是可以的。
当我将 Global 作为新站点时,我不想将 North 作为 Global 站点的一部分。
示例,添加名为 (NEWTEST) 的站点 2,即全局
位置站点配置table
OID LOCATIONDETAILOID SITENAME
1 2 TEST
2 1 NEWTEST
上面的查询returns
全球、北方、北方 1、南方、南方 1
又是 NORTH(TEST) AND NORTH1(记录副本)
我希望查询 return
全球、北方(测试)、北方 1、南方、南方 1
如果 child 已经创建了网站,查询应该被忽略。请帮忙
将您的 CTE 更改为以 GLOBAL 开头作为锚定成员。递归部分需要调整以包括站点名称,这是通过合并(如果位置没有站点名称)和子查询来完成的。
with LOCALSITEHIERARCHY
as (
select A.OID
, A.PARENTOID
, cast(A.LOCATIONNAME+' ( '+LSC.SITENAME+' )' as nvarchar(100)) as NAME
, LSC.OID as SITEOID
, cast(row_number() over(partition by parentoid order by A.LOCATIONNAME) as varchar(max)) as [PATH]
from LOCATIONDETAIL as A
inner join LOCATIONSITECONFIG as LSC on LSC.LOCATIONDETAILOID = A.OID
where 1 = 1
and A.PARENTOID is null
and LSC.RECSTATUS = 'A'
union all
select A.OID
, A.PARENTOID
, cast(coalesce(A.LOCATIONNAME+' ( '+
(
select SITENAME
from LOCATIONSITECONFIG C
where C.LOCATIONDETAILOID = A.OID
)+' )', A.LOCATIONNAME) as nvarchar(100)) as NAME
, coalesce((select C.OID from LOCATIONSITECONFIG C where C.LOCATIONDETAILOID = A.OID),NULL) as SITEOID
, [path]+'-'+cast(row_number() over(partition by A.parentoid order by A.LOCATIONNAME) as varchar(max))
from LOCATIONDETAIL as A
inner join LOCALSITEHIERARCHY as LH on A.PARENTOID = LH.OID)
select * from LOCALSITEHIERARCHY order by [PATH];
结果输出如下图所示。
我正在使用 SQL Server 2012
位置详情table
OID LOCATIONNAME PARENTID
1 GLOBAL 0
2 NORTH 1
3 SOUTH 1
4 NORTH1 2
5 SOUTH1 3
位置站点配置table
OID LOCATIONID SITENAME
1 2 TEST
我使用递归 CTE 查询
;WITH LOCALSITEHIERARCHY AS
(
SELECT A.OID
,A.PARENTOID
,CAST(A.LOCATIONNAME + ' ( ' + LSC.SITENAME + ' )' AS NVARCHAR(100)) AS NAME
,LSC.OID AS SITEOID
FROM LOCATIONDETAIL A
INNER JOIN LOCATIONSITECONFIG LSC
ON LSC.LOCATIONDETAILOID = A.OID
WHERE
LSC.SITENAME <> 'GLOBAL' AND LSC.RECSTATUS = 'A'
UNION ALL
SELECT
A.OID
,A.PARENTOID
,CAST(A.LOCATIONNAME AS NVARCHAR(100))
,LH.SITEOID
FROM LOCATIONDETAIL A
INNER JOIN LOCALSITEHIERARCHY LH ON A.PARENTOID = LH.OID
)
SELECT * FROM LOCALSITEHIERARCHY
NORTH 现在是 Global.This 中的一个单独站点 查询 returns North 和 North 1 在树结构中是可以的。
当我将 Global 作为新站点时,我不想将 North 作为 Global 站点的一部分。
示例,添加名为 (NEWTEST) 的站点 2,即全局
位置站点配置table
OID LOCATIONDETAILOID SITENAME
1 2 TEST
2 1 NEWTEST
上面的查询returns 全球、北方、北方 1、南方、南方 1 又是 NORTH(TEST) AND NORTH1(记录副本)
我希望查询 return 全球、北方(测试)、北方 1、南方、南方 1
如果 child 已经创建了网站,查询应该被忽略。请帮忙
将您的 CTE 更改为以 GLOBAL 开头作为锚定成员。递归部分需要调整以包括站点名称,这是通过合并(如果位置没有站点名称)和子查询来完成的。
with LOCALSITEHIERARCHY
as (
select A.OID
, A.PARENTOID
, cast(A.LOCATIONNAME+' ( '+LSC.SITENAME+' )' as nvarchar(100)) as NAME
, LSC.OID as SITEOID
, cast(row_number() over(partition by parentoid order by A.LOCATIONNAME) as varchar(max)) as [PATH]
from LOCATIONDETAIL as A
inner join LOCATIONSITECONFIG as LSC on LSC.LOCATIONDETAILOID = A.OID
where 1 = 1
and A.PARENTOID is null
and LSC.RECSTATUS = 'A'
union all
select A.OID
, A.PARENTOID
, cast(coalesce(A.LOCATIONNAME+' ( '+
(
select SITENAME
from LOCATIONSITECONFIG C
where C.LOCATIONDETAILOID = A.OID
)+' )', A.LOCATIONNAME) as nvarchar(100)) as NAME
, coalesce((select C.OID from LOCATIONSITECONFIG C where C.LOCATIONDETAILOID = A.OID),NULL) as SITEOID
, [path]+'-'+cast(row_number() over(partition by A.parentoid order by A.LOCATIONNAME) as varchar(max))
from LOCATIONDETAIL as A
inner join LOCALSITEHIERARCHY as LH on A.PARENTOID = LH.OID)
select * from LOCALSITEHIERARCHY order by [PATH];
结果输出如下图所示。