select * from table1
a1(病房) a2(人数) a3(1代表爱滋病、1代表感冒、3代表骨折)
1001 5 1
1001 6 2
1001 7 3
有没有办法实现输出下面的格式:病房 爱滋病人数 感冒人数 骨折人数
1001 5 6 7也就是三行汇总成一行?、、
a1(病房) a2(人数) a3(1代表爱滋病、1代表感冒、3代表骨折)
1001 5 1
1001 6 2
1001 7 3
有没有办法实现输出下面的格式:病房 爱滋病人数 感冒人数 骨折人数
1001 5 6 7也就是三行汇总成一行?、、
--用于:交叉表的列数是确定的
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 table1
group by a1
把''号去掉后 也有问题,
else后面一定是0吗?谢谢回复
------------------------------
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',9,'2' select * from test
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 a1drop table test
else后面 0的意思是指(1代表爱滋病、1代表感冒、3代表骨折)条件之外
的情况
select * from table1
a1(病房) a2(人数) a3(1代表爱滋病、1代表感冒、3代表骨折)
1001 5 1
1001 6 2
1001 7 3
1002 4 1
1002 5 2
1002 8 3那么好像是求和不用SUM可以吗?用别的什么呢?
不过小弟还是个菜鸟,无赖单位其他人比我还菜,被迫的。
我把这个重新发个详细标题,yizhi和飘飘再看看呀