27,579
社区成员
发帖
与我相关
我的任务
分享
create table #t(
layer int, -- 等级
departmentid varchar(10), -- 部门ID
department varchar(40), -- 部门名称
upperid varchar(10) -- 上级部门
)
insert into #t
select 1,'aa','1234','' union all
select 2,'aaa','12345','aa' union all
select 1,'bb','4321','' union all
select 3,'aaaa','123456','aaa' union all
select 2,'bbb','43211','bb' union all
select 3,'bbbb','432112','bbb' union all
select 1,'cc','2341','' union all
select 3,'cccc','23411','ccc' union all
select 2,'ccc','2341111','cc' union all
select 3,'bbc','32123','bb'
with cet
as
(
select departmentid,department,upperid from #t
union all
select b.departmentid,b.department,b.upperid from cet a,#t b
where b.departmentid=a.upperid
)
select departmentid,department,count(departmentid) as num from cet
group by departmentid,department
/*
departmentid department num
------------ ---------------------------------------- -----------
aa 1234 3
aaa 12345 2
aaaa 123456 1
cc 2341 3
cccc 23411 1
ccc 2341111 2
bbc 32123 1
bb 4321 4
bbb 43211 2
bbbb 432112 1
(10 行受影响)
/*
;with T
as
(select *,pathfile=cast(departmentid as nvarchar(400)) from #t a where not exists(select 1 from #t where departmentid=a.upperid)
union all
select a.*,pathfile=cast(b.pathfile+'\'+a.departmentid as nvarchar(400)) from #T a join T b on b.departmentid=a.upperid)
select
a.departmentid,a.department,count(b.layer) as 記錄
from
T a
left join
T b on b.pathfile like a.pathfile+'\%'--不包含自身
group by a.departmentid,a.department
departmentid department 記錄
------------ ---------------------------------------- -----------
aa 1234 2
aaa 12345 1
aaaa 123456 0
cc 2341 2
cccc 23411 0
ccc 2341111 1
bbc 32123 0
bb 4321 3
bbb 43211 1
bbbb 432112 0
(10 個資料列受到影響)
if object_id('tb') is not null
drop table tb
go
create table tb(
layer int, -- 等级
departmentid varchar(10), -- 部门ID
department varchar(40), -- 部门名称
upperid varchar(10) -- 上级部门
)
insert into tb
select 1,'aa','1234','' union all
select 2,'aaa','12345','aa' union all
select 1,'bb','4321','' union all
select 3,'aaaa','123456','aaa' union all
select 2,'bbb','43211','bb' union all
select 3,'bbbb','432112','bbb' union all
select 1,'cc','2341','' union all
select 3,'cccc','23411','ccc' union all
select 2,'ccc','2341111','cc' union all
select 3,'bbc','32123','bb'
if object_id('get_count') is not null
drop function get_count
go
CREATE FUNCTION get_count(@ID char(3))
RETURNS int
AS
BEGIN
declare @t_Level TABLE(ID char(20),Level int)
DECLARE @Level int
SET @Level=1
INSERT @t_Level SELECT @ID,@Level
WHILE @@ROWCOUNT>0
BEGIN
SET @Level=@Level+1
INSERT @t_Level SELECT a.departmentid,@Level
FROM tb a,@t_Level b
WHERE a.upperid=b.ID
AND b.Level=@Level-1
END
RETURN (select count(1) from @t_Level )
END
go
select departmentid ,department,
dbo.get_count(departmentid) as 子部门个数
from tb
group by departmentid ,department
--测试数据
CREATE TABLE tb(ID char(3),PID char(3),Name nvarchar(10))
INSERT tb SELECT '001',NULL ,'山东省'
UNION ALL SELECT '002','001','烟台市'
UNION ALL SELECT '004','002','招远市'
UNION ALL SELECT '003','001','青岛市'
UNION ALL SELECT '005',NULL ,'四会市'
UNION ALL SELECT '006','005','清远市'
UNION ALL SELECT '007','006','小分市'
GO
--查询指定节点及其所有子节点的函数
CREATE FUNCTION f_Cid(@ID char(3))
RETURNS @t_Level TABLE(ID char(3),Level int)
AS
BEGIN
DECLARE @Level int
SET @Level=1
INSERT @t_Level SELECT @ID,@Level
WHILE @@ROWCOUNT>0
BEGIN
SET @Level=@Level+1
INSERT @t_Level SELECT a.ID,@Level
FROM tb a,@t_Level b
WHERE a.PID=b.ID
AND b.Level=@Level-1
END
RETURN
END
GO
--调用函数查询002及其所有子节点
SELECT a.*
FROM tb a,f_Cid('002') b
WHERE a.ID=b.ID
/*--结果
ID PID Name
------ ------- ----------
002 001 烟台市
004 002 招远市
--*/