日期:2014-05-17 浏览次数:20532 次
--就是展bom哦
declare @t1 table(id int,name nvarchar(10),fid int)
insert into @t1 values(1,N'廣州',null)
insert into @t1 values(2,N'天河',1)
insert into @t1 values(3,N'越秀',1)
insert into @t1 values(4,N'白雲',1)
insert into @t1 values(5,N'太和',4)
insert into @t1 values(6,N'人和',4)
declare @t2 table (areaid int,buscount int)
insert into @t2 values(1,100)
insert into @t2 values(2,120)
insert into @t2 values(3,150)
insert into @t2 values(4,120)
insert into @t2 values(5,50)
insert into @t2 values(6,20)
;with cte
as
(
select id as topid,id,name
from @t1 x1
union all
select topid,B.id,A.name
from cte A ,@t1 B
where A.id=B.fid
)
select A.topid,A.name,sum(isnull(B.buscount,0)) as buscount
from cte A
left join @t2 B
on A.id=B.areaid
group by A.topid,A.name
order by 1
/*
1 廣州 560
2 天河 120
3 越秀 150
4 白雲 190
5 太和 50
6 人和 20
*/