表 A 部门
deptId deptName
1 办公室
2 开发部
3 人事部表 B 部门人员关联
deptId userId
1 1000
2 1001
3 1003
2 1002表c 人员
userId userName
1000 张三
1001 李四
1002 王五
1003 赵六
表d 会议
id meetPerson
1 李四,赵六
2 张三,李四,王五现在想得根据部门来统计每个部门的人员参加会议的次数结果
----------------------------------------------------
部门名称 参加会议次数
办公室 1
开发部 3
人事部 1请问如何写能简便些
deptId deptName
1 办公室
2 开发部
3 人事部表 B 部门人员关联
deptId userId
1 1000
2 1001
3 1003
2 1002表c 人员
userId userName
1000 张三
1001 李四
1002 王五
1003 赵六
表d 会议
id meetPerson
1 李四,赵六
2 张三,李四,王五现在想得根据部门来统计每个部门的人员参加会议的次数结果
----------------------------------------------------
部门名称 参加会议次数
办公室 1
开发部 3
人事部 1请问如何写能简便些
1 李四,赵六
2 张三,李四,王五--
这,重名怎么办
if object_id('tempdb.dbo.#a') is not null drop table #a
create table #a(deptId int, deptName varchar(8))
insert into #a
select 1, '办公室' union all
select 2, '开发部' union all
select 3, '人事部'
--> 测试数据:#b
if object_id('tempdb.dbo.#b') is not null drop table #b
create table #b(deptId int, userId int)
insert into #b
select 1, 1000 union all
select 2, 1001 union all
select 3, 1003 union all
select 2, 1002
--> 测试数据:#c
if object_id('tempdb.dbo.#c') is not null drop table #c
create table #c(userId int, userName varchar(8))
insert into #c
select 1000, '张三' union all
select 1001, '李四' union all
select 1002, '王五' union all
select 1003, '赵六'
--> 测试数据:#d
if object_id('tempdb.dbo.#d') is not null drop table #d
create table #d(id int, meetPerson varchar(14))
insert into #d
select 1, '李四,赵六' union all
select 2, '张三,李四,王五'select a.deptName, count(1)cn from #a a left join
#b b join #c c on b.userId=c.userId join #d d on ','+d.meetPerson+',' like '%,'+c.userName+',%'
on a.deptId = b.deptId group by a.deptName
/*
结果
----------------------------------------------------
部门名称 参加会议次数
办公室 1
开发部 3
人事部 1
*/
from tbB a,
(select userId,@count=(select count(1) from tbc
where CHARINDEX(','+userName+',',','+meetPerson+',')>0) from tbC ) b
where a.userId=b.userId
from #b a,#a c,
(select userId,count1=(select count(1) from #d
where CHARINDEX(','+a.userName+',',','+meetPerson+',')>0) from #c a ) b
where a.userId=b.userId and a.deptid=c.deptid
group by c.deptNamedeptName 次数
-------- -----------
办公室 1
开发部 3
人事部 1
declare @部门 table (deptId int,deptName varchar(6))
insert into @部门
select 1,'办公室' union all
select 2,'开发部' union all
select 3,'人事部'declare @部门人员关联 table (deptId int,userId int)
insert into @部门人员关联
select 1,1000 union all
select 2,1001 union all
select 3,1003 union all
select 2,1002declare @人员 table (userId int,userName varchar(4))
insert into @人员
select 1000,'张三' union all
select 1001,'李四' union all
select 1002,'王五' union all
select 1003,'赵六'declare @会议 table (id int,meetPerson varchar(14))
insert into @会议
select 1,'李四,赵六' union all
select 2,'张三,李四,王五'SELECT deptName ,
COUNT(1) AS 人数
FROM ( SELECT a.*
FROM @人员 c
RIGHT JOIN @会议 d ON d.meetPerson LIKE '%' + c.userName
+ '%'
LEFT JOIN @部门人员关联 b ON c.userId = b.userId
LEFT JOIN @部门 a ON b.deptId = a.deptId
) aa
GROUP BY deptName
/*
deptName 人数
-------- -----------
办公室 1
开发部 3
人事部 1
*/
insert into a
select 1, '办公室' union all
select 2, '开发部' union all
select 3, '人事部'
create table b(deptId int, userId int)
insert into b
select 1, 1000 union all
select 2, 1001 union all
select 3, 1003 union all
select 2, 1002
create table c(userId int, userName varchar(8))
insert into c
select 1000, '张三' union all
select 1001, '李四' union all
select 1002, '王五' union all
select 1003, '赵六'
create table d(id int, meetPerson varchar(14))
insert into d
select 1, '李四,赵六' union all
select 2, '张三,李四,王五'select a.deptName , count(1) 参加会议次数
from a , b, c , d
where a.deptId = b.deptId and b.userId = c.userId and
charindex(',' + c.userName + ',' , ',' + d.meetPerson + ',') > 0
group by a.deptNamedrop table a , b, c, d/*
deptName 参加会议次数
-------- -----------
办公室 1
开发部 3
人事部 1(所影响的行数为 3 行)
*/