create table #Tmp
(
ID int IDENTITY (1,1) not null,
name varchar(50),
sl int
);
DECLARE @ID VARCHAR(36)
SET @ID = '7197ccfb-f01d-437f-9059-f4b821ebf2e1'
;WITH T AS
(
SELECT ID , parentid , NAME
FROM Base_SXZD
WHERE ID = @ID
UNION ALL
SELECT A.ID , A.parentid , A.NAME
FROM Base_SXZD AS A JOIN T AS B ON A.parentid = B.id
)
insert into #Tmp
select name=(select name from Base_SXZD where id='7197ccfb-f01d-437f-9059-f4b821ebf2e1'),sl = sum(m.sl) from (
select a.id, a.name,count(b.id) as sl from T a
left join jxxt_ajlb b on a.id = b.ajlxid
" + GetIfWhere(all, ajly) + @"
group by a.id,a.name) m
SET @ID = '5090a923-3c1a-4628-95ae-12edf10d41ae'
;WITH T AS
(
SELECT ID , parentid , NAME
FROM Base_SXZD
WHERE ID = @ID
UNION ALL
SELECT A.ID , A.parentid , A.NAME
FROM Base_SXZD AS A JOIN T AS B ON A.parentid = B.id
)
insert into #Tmp
select name=(select name from Base_SXZD where id='5090a923-3c1a-4628-95ae-12edf10d41ae'),sl = sum(m.sl) from (
select a.id, a.name,count(b.id) as sl from T a
left join jxxt_ajlb b on a.id = b.ajlxid
" + GetIfWhere(all, ajly) + @"
group by a.id,a.name) m
SET @ID = '91322101-c2cb-47bd-91ce-69418722d835'
;WITH T AS
(
SELECT ID , parentid , NAME
FROM Base_SXZD
WHERE ID = @ID
UNION ALL
SELECT A.ID , A.parentid , A.NAME
FROM Base_SXZD AS A JOIN T AS B ON A.parentid = B.id
)
insert into #Tmp
select name=(select name from Base_SXZD where id='91322101-c2cb-47bd-91ce-69418722d835'),sl = sum(m.sl) from (
select a.id, a.name,count(b.id) as sl from T a
left join jxxt_ajlb b on a.id = b.ajlxid
" + GetIfWhere(all, ajly) + @"
group by a.id,a.name) m
select ID,name, isnull(sl,0) as ajsl from #Tmp
drop table #Tmp