部门 编号
A1 001
A1 002
A101 003
A101 004
A101 005
B1 006
B1 007
B1 008
B101 009
C1 010
C1 011
C1 012
C1 013
实现效果
A1 5
A101 3
B1 4
B101 1
C1 4
------解决方案--------------------
呵呵,你太实诚了,贴了那么多的数据,我就模拟了几条数据哈,是这个效果吗:

--drop table t1
--drop table t2
create table t1(
部门ID int,
部门代码 varchar(20),
上级代码 varchar(10),
部门名称 varchar(20)
)
insert into t1
select 2, '0101', '01', '管理部'
union all select 3, '0102', '01', '市场支持部'
union all select 4, '0103', '01', '技术部'
union all select 7, '010101', '0101', '总务课'
union all select 8, '010102', '0101', '财务课'
union all select 9, '010201', '0102', '市场企划组'
create table t2
(
人员ID int, 部门ID int, 人员编号 varchar(20)
)
insert into t2
select 2, 2, '79503001'
union all select 3, 4, '79408005'
union all select 4, 7, '70911001'
union all select 5, 8, '71002004'
union all select 6, 9, '71002003'
union all select 7, 8, '71002002'
;with t
as
(
select t1.部门ID,
t1.部门代码,
t1.上级代码,
t1.部门名称,
count(*) as people_count
from t1
left join t2
on t1.部门ID = t2.部门ID
group by t1.部门ID,
t1.部门代码,
t1.上级代码,
t1.部门名称
)
select 部门ID,
部门代码,
(select sum(people_count)
from t t2
where t2.部门代码 like t1.部门代码 +'%') as 人数
from t t1
/*
部门ID 部门代码 人数
2 0101 4
3 0102 2
4 0103 1
7 010101 1
8 010102 2
9 010201 1
*/