查询行变列遇到的语句问题

smaworm 2008-06-12 02:01:10
有三个表
1. t_b_Group 此表存储的是部门分组 结构如下
f_GroupID f_GroupName
1 总经理室
2 管理人员
3 维修工
....
2 t_b_Consumer 此表存储员工信息 主要结构如下
f_GroupID (部门) f_ConsumerID(员工号)
1 1001
2 1002
2 1003
3 1004
3 InMine 此表存储某工作车间内人员情况
f_ConsumerID(员工号) f_ReadDate (进入时间)
1001 2008-06-12 18:04:32
1002 2008-06-12 11:04:44
1003 2008-06-12 15:06:55
想要查询出结果如下(某工作车间内当前的各部门人数)
中段 总经理室 管理人员 维修工

数量 1 2 0

应怎么写查询,我自己写了一个 可是当某个部门的人数为0时就不显示了,要求是不管此部门在车间内有没有人,都要显示
我自己写的语句如下,高手指点
declare @sql1 varchar(8000),@sql2 varchar(8000)
select @sql1='select [中段]=''数量'''
select @sql1=@sql1+',['+中段+']= sum(case 中段 when '''+中段+''' then 数量 end)' from (select count(1) 数量,g.f_GroupName 中段
from InMine i,t_b_Consumer c,t_b_Group g
where i.f_ConsumerID = c.f_ConsumerID and
c.f_GroupID = g.f_GroupID
group by g.f_GroupName) tb1
set @sql2=@sql1+' from (select count(1) 数量,g.f_GroupName 中段
from InMine i,t_b_Consumer c,t_b_Group g
where i.f_ConsumerID = c.f_ConsumerID and
c.f_GroupID = g.f_GroupID
group by g.f_GroupName) tb2'
exec(@sql2)
结果为
中段 总经理室 管理人员
数量 1 2
...全文
90 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ojuju10 2008-06-12
  • 打赏
  • 举报
回复

用left join




declare @sql1 varchar(8000),@sql2 varchar(8000)
select @sql1='select [中段]=''数量'''
select @sql1=@sql1+',['+中段+']= isnull(sum(case 中段 when '''+中段+''' then 数量 end),0)' from (select count(1) 数量,g.f_GroupName 中段
from t_b_Consumer c left join InMine i on i.f_ConsumerID = c.f_ConsumerID
left join t_b_Group g
on c.f_GroupID = g.f_GroupID
group by g.f_GroupName) tb1
set @sql2=@sql1+' from (select count(1) 数量,g.f_GroupName 中段
from InMine i,t_b_Consumer c,t_b_Group g
where i.f_ConsumerID = c.f_ConsumerID and
c.f_GroupID = g.f_GroupID
group by g.f_GroupName) tb2'
exec(@sql2)

zanyzyg 2008-06-12
  • 打赏
  • 举报
回复


create table #t_b_Group(f_GroupID int, f_GroupName varchar(20))

insert into #t_b_Group
select 1 as f_GroupID, '总经理室 ' as f_GroupName
union all
select 2, '管理人员 '
union all
select 3, '维修工 '

create table #t_b_Consumer(f_GroupID int, f_ConsumerID varchar(10))
insert into #t_b_Consumer
select 1 as f_GroupID, '1001' as f_ConsumerID
union all
select 2, '1002'
union all
select 2, '1003'
union all
select 3, '1004'

create table #InMine(f_ConsumerID varchar(10), f_ReadDate datetime)
insert into #InMine
select '1001' as f_ConsumerID, '2008-06-12 18:04:32'
union all
select '1002', '2008-06-12 11:04:44'
union all
select '1003', '2008-06-12 15:06:55'

declare @sql varchar(1000)
set @sql='select ''数量'' as 中段'

select @sql=@sql+',sum(case when f_GroupID='+convert(varchar(10),f_GroupID)+' then 1 else 0 end) as '''+f_GroupName+'''' from #t_b_Group


set @sql=@sql+' from (select #t_b_Consumer.f_GroupID,#InMine.f_ConsumerID from #t_b_Consumer,#InMine where #t_b_Consumer.f_ConsumerID=#InMine.f_ConsumerID)t'

exec(@sql)

drop table #t_b_Group
drop table #t_b_Consumer
drop table #InMine

数量 1 2 0

中国风 2008-06-12
  • 打赏
  • 举报
回复
DECLARE @S NVARCHAR(1000)
SET @S=''
SELECT @S=@S+','+QUOTENAME(f_GroupName)+'=SUM(CASE WHEN f_GroupID='+RTRIM(f_GroupID)+' THEN 1 else 0 END)'
from t_b_Group
exec ('select [中段]=N''數量'''+@s+' from t_b_Consumer a where exists(select 1 from InMine where f_ConsumerID=a.f_ConsumerID)')
zanyzyg 2008-06-12
  • 打赏
  • 举报
回复

行转换信息直接从表t_b_Group里面取就可以了啊,为什么从关联后的结果信息里面取啊?

smaworm 2008-06-12
  • 打赏
  • 举报
回复
能不能说清楚点 最好有语句
子陌红尘 2008-06-12
  • 打赏
  • 举报
回复
将内联接换成左外联接。

left join ...

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧