----创建测试数据
declare @t1 table(a1 varchar(10),a2 int,a3 int)
insert @t1
select '1001',5,1 union all
select '1001',6,2 union all
select '1001',7,3 union all
select '1002',4,1 union all
select '1002',5,2 union all
select '1002',8,3
declare @t2 table(a1 varchar(10),a10 varchar(20),a11 varchar(50))
insert @t2
select '1001','小李飞刀','88888888' union all
select '1002','老李飞叉','99999999' union all
select '1003','飞刀飞叉','66666666'
----汇总
select a.a1 as 病房,b.a10 as 科长, a.艾滋病人数,a.感冒人数,a.骨折人数 from
(
select a1 ,
艾滋病人数= sum(case a3 when 1 then a2 else 0 end),
感冒人数= sum(case a3 when 2 then a2 else 0 end),
骨折人数= sum(case a3 when 3 then a2 else 0 end)
from @t1 group by a1
) a /*将病房患者统计这个子查询再外联一下病房负责人表就行了*/
left join @t2 b on a.a1 = b.a1
declare @t1 table(a1 varchar(10),a2 int,a3 int)
insert @t1
select '1001',5,1 union all
select '1001',6,2 union all
select '1001',7,3 union all
select '1002',4,1 union all
select '1002',5,2 union all
select '1002',8,3
declare @t2 table(a1 varchar(10),a10 varchar(20),a11 varchar(50))
insert @t2
select '1001','小李飞刀','88888888' union all
select '1002','老李飞叉','99999999' union all
select '1003','飞刀飞叉','66666666'
----汇总
select a.a1 as 病房,b.a10 as 科长, a.艾滋病人数,a.感冒人数,a.骨折人数 from
(
select a1 ,
艾滋病人数= sum(case a3 when 1 then a2 else 0 end),
感冒人数= sum(case a3 when 2 then a2 else 0 end),
骨折人数= sum(case a3 when 3 then a2 else 0 end)
from @t1 group by a1
) a /*将病房患者统计这个子查询再外联一下病房负责人表就行了*/
left join @t2 b on a.a1 = b.a1
from table2
left join table1 A on A.a1=table2.a1 and A.a3=1
left join table1 B on B.a1=table2.a1 and B.a3=2
left join table1 C on C.a1=table2.a1 and C.a3=3
-------------------------------
create table test
(a1 char(4),
a2 int,
a3 char(1))
insert test(a1,a2,a3)
select '1001',5,'1' union all
select '1001',6,'2' union all
select '1001',7,'3' union all
select '1002',4,'1' union all
select '1002',5,'2' union all
select '1002',8,'3'
--select * from testcreate table test2
(a1 char(4),
a10 varchar(10),
a11 varchar(20))
insert test2
select '1001','小李飞刀','88888888' union all
select '1002','老李飞刀','99999999' union all
select '1003','飞刀飞叉','66666666'
--select * from test2--把第一帖查询出来的结果当作一个表与表二作连接
select B.a1 as 病房,B.a10 as 科长,A.爱滋病,A.感冒,A.骨折
from test2 B
inner join
(select a1,sum(case a3 when 1 then a2 else 0 end) as '爱滋病',
sum(case a3 when 2 then a2 else 0 end) as '感冒',
sum(case a3 when 3 then a2 else 0 end) as '骨折'
from test
group by a1)A
on B.a1=A.a1drop table test,test2
sum(case a3 when '1' then a.a1 else 0 end) as 爱滋病人人数,
sum(case a3 when '2' then a.a1 else 0 end) as 感冒人数,
sum(case a3 when '3' then a.a1 else 0 end) as 骨折人数
from table1 as a ,table2 as b
where a.a1 =b.a1
group by a.a1,b.a10
可能是版本或解释器的不同,yizhi的在我这用不成,比如
select a1,sum(case a3 when 1 then a2 else 0 end) as '爱滋病',
这里,when后的面1一定要加'',else后面的0也要加'',后面的'爱滋病'的''不能加,不然出错
QQMagicer的else后面的0没加'',
另外inner我没用过,不知道有没有材料提供给我研究一下,还有最后一个问题,
select a.a1,b.a10 from tabl1 as a,table2 as b where a.a1 =b.a1 group by a.a1,b.a10老是提示在第一个a.a1上,说非单分组函数,是怎么回事?
在此先感谢yizhi,QQMagicer,hellowork,playwarcraft,等众多高手的指点,受益非浅!!!!