有一表
id parentid .......
1 0
2 1
3 1
4 0
5 4
6 0
...............
希望统计实现比如由上表:
节点 子点数
1 2
2 0
3 0
4 1
5 0
6 0
..................
谢谢,请教!!
------解决方案--------------------
create table tb(id int,parentid int)
insert into tb values(1, 0)
insert into tb values(2, 1)
insert into tb values(3, 1)
insert into tb values(4, 0)
insert into tb values(5, 4)
insert into tb values(6, 0)
go
select a.id 节点,isnull(b.子点数,0) 子点数 from tb a
left join
(select parentid,count(*) 子点数 from tb group by parentid) b
on a.id = b.parentid
drop table tb
/*
点 子点数
----------- -----------
1 2
2 0
3 0
4 1
5 0
6 0
(所影响的行数为 6 行)
*/