我不明白为什么我会得到双行
;with cteP as (
Select GROUNDID
,OBJECTID
,MAINGROUNDID
,PathID = cast(GROUNDID as varchar(max))
From PMEGROUND
Where REPLACE(ltrim(rtrim(MAINGROUNDID)),' ',NULL) is Null
--AND DATAAREAID = 'sgv'
Union All
Select GROUNDID = r.GROUNDID
,OBJECTID = r.OBJECTID
,MAINGROUNDID = r.MAINGROUNDID
,PathID = p.PathID+concat(',',cast(r.GROUNDID as varchar(max)))
From PMEGROUND r
Join cteP p on r.MAINGROUNDID = p.GROUNDID)
Select
PMEOBJECT.OBJECTID,
--PMEOBJECT.GROUNDID,
--cteP.GROUNDID,
B.Lvl1
,B.Lvl2
,B.Lvl3
,B.Lvl4
FROM PMEOBJECT
Left Join cteP
ON cteP.GROUNDID = PMEOBJECT.GROUNDID
Cross Apply (
Select Lvl1 = xDim.value('/x[1]','varchar(50)')
,Lvl2 = xDim.value('/x[2]','varchar(50)')
,Lvl3 = xDim.value('/x[3]','varchar(50)')
,Lvl4 = xDim.value('/x[4]','varchar(50)')
From ( values (cast('<x>' + replace(PathID,',','</x><x>')+'</x>' as xml))) B(xDim)
) B
Where cteP.GROUNDID IS NOT NULL
Order By OBJECTID
编辑:这是我当前的数据集:
CREATE TABLE PMEGROUND (
OBJECTID nvarchar(20),
GROUNDID nvarchar(20),
MAINGROUNDID nvarchar(20)
);
insert into PMEGROUND Values ('2','6','')
insert into PMEGROUND Values ('3','7','')
insert into PMEGROUND Values ('4','8','')
insert into PMEGROUND Values ('5','9','')
insert into PMEGROUND Values ('6','10','')
insert into PMEGROUND Values ('7','11','')
insert into PMEGROUND Values ('8','14','')
insert into PMEGROUND Values ('9','15','')
insert into PMEGROUND Values ('10','16','')
insert into PMEGROUND Values ('11','17','')
insert into PMEGROUND Values ('12','18','')
insert into PMEGROUND Values ('13','19','')
insert into PMEGROUND Values ('14','20','')
insert into PMEGROUND Values ('15','21','')
insert into PMEGROUND Values ('16','22','')
insert into PMEGROUND Values ('17','23','')
insert into PMEGROUND Values ('18','24','')
insert into PMEGROUND Values ('19','25','')
insert into PMEGROUND Values ('20','26','')
insert into PMEGROUND Values ('12669','2610','26')
insert into PMEGROUND Values ('12670','2620','26')
insert into PMEGROUND Values ('12671','2630','26')
insert into PMEGROUND Values ('12672','2640','26')
insert into PMEGROUND Values ('12673','2650','26')
insert into PMEGROUND Values ('21','27','')
insert into PMEGROUND Values ('22','28','')
insert into PMEGROUND Values ('23','29','')
insert into PMEGROUND Values ('24','30','')
insert into PMEGROUND Values ('12674','3010','30')
insert into PMEGROUND Values ('12675','3020','30')
insert into PMEGROUND Values ('12676','3030','30')
insert into PMEGROUND Values ('12677','3040','30')
CREATE TABLE PMEOBJECT (
OBJECTID nvarchar(20),
GROUNDID nvarchar(20)
);
insert into PMEOBJECT Values ('1','')
insert into PMEOBJECT Values ('2','6')
insert into PMEOBJECT Values ('3','7')
insert into PMEOBJECT Values ('4','8')
insert into PMEOBJECT Values ('5','9')
insert into PMEOBJECT Values ('6','10')
insert into PMEOBJECT Values ('7','11')
insert into PMEOBJECT Values ('8','14')
insert into PMEOBJECT Values ('9','15')
insert into PMEOBJECT Values ('10','16')
insert into PMEOBJECT Values ('11','17')
insert into PMEOBJECT Values ('13','19')
insert into PMEOBJECT Values ('14','20')
insert into PMEOBJECT Values ('15','21')
insert into PMEOBJECT Values ('16','22')
insert into PMEOBJECT Values ('17','23')
insert into PMEOBJECT Values ('18','24')
insert into PMEOBJECT Values ('19','25')
insert into PMEOBJECT Values ('20','26')
insert into PMEOBJECT Values ('12669','2610')
insert into PMEOBJECT Values ('12670','2620')
insert into PMEOBJECT Values ('12671','2630')
insert into PMEOBJECT Values ('12672','2640')
insert into PMEOBJECT Values ('12673','2650')
insert into PMEOBJECT Values ('21','27')
insert into PMEOBJECT Values ('22','28')
insert into PMEOBJECT Values ('23','29')
insert into PMEOBJECT Values ('24','30')
insert into PMEOBJECT Values ('12674','3010')
insert into PMEOBJECT Values ('12675','3020')
insert into PMEOBJECT Values ('12676','3030')
insert into PMEOBJECT Values ('12677','3040')
编辑:结果数据集的样子:
OBJECTID,GROUNDID,Lvl1,Lvl2,Lvl3,Lvl4
1 NULL NULL NULL NULL NULL
10 016 016 NULL NULL NULL
11 017 017 NULL NULL NULL
12669 02610 02610 NULL NULL NULL
12669 02610 026 02610 NULL NULL
12670 02620 026 02620 NULL NULL
12670 02620 02620 NULL NULL NULL
12671 02630 02630 NULL NULL NULL
12671 02630 026 02630 NULL NULL
12672 02640 026 02640 NULL NULL
12672 02640 02640 NULL NULL NULL
12673 02650 02650 NULL NULL NULL
12673 02650 026 02650 NULL NULL
12674 03010 030 03010 NULL NULL
12674 03010 03010 NULL NULL NULL
12675 03020 03020 NULL NULL NULL
12675 03020 030 03020 NULL NULL
12676 03030 030 03030 NULL NULL
12676 03030 03030 NULL NULL NULL
12677 03040 03040 NULL NULL NULL
12677 03040 030 03040 NULL NULL
13 019 019 NULL NULL NULL
14 020 020 NULL NULL NULL
15 021 021 NULL NULL NULL
16 022 022 NULL NULL NULL
17 023 023 NULL NULL NULL
18 024 024 NULL NULL NULL
19 025 025 NULL NULL NULL
2 006 006 NULL NULL NULL
20 026 026 NULL NULL NULL
21 027 027 NULL NULL NULL
22 028 028 NULL NULL NULL
23 029 029 NULL NULL NULL
24 030 030 NULL NULL NULL
3 007 007 NULL NULL NULL
4 008 008 NULL NULL NULL
5 009 009 NULL NULL NULL
6 010 010 NULL NULL NULL
7 011 011 NULL NULL NULL
8 014 014 NULL NULL NULL
9 015 015 NULL NULL NULL
原始表PMEGround和PMEObject的工作方式如下:
关于我为什么在查询中有双行的任何想法? OBJECTID应该保持唯一。
亲切的问候, 伊戈尔(Igor)