类型 编号 vit次数 pit次数 ait 次数 pit 1001 2 1 1
vit 1001 2 1 1
vit 1001 2 1 1
ait 1001 2 1 1
vit 1002 1 0 0
如上表所示,查出来某一个编号对应的三种类型,分别出现的次数做一个统计,搞了一下午,么搞出来。
请大侠们帮帮忙谢谢
vit 1001 2 1 1
vit 1001 2 1 1
ait 1001 2 1 1
vit 1002 1 0 0
如上表所示,查出来某一个编号对应的三种类型,分别出现的次数做一个统计,搞了一下午,么搞出来。
请大侠们帮帮忙谢谢
http://bbs.csdn.net/topics/390451420
这么多都是你问的,应该都会写了
类型 编号 vit次数 pit次数 ait 次数 pit 1001 2 1 1
vit 1001 2 1 1
vit 1001 2 1 1
ait 1001 2 1 1
vit 1002 1 0 0
这个就是结果
pit 1001
vit 1001
vit 1001
ait 1001
vit 1002
上边是数据,然后查询结果是这个:
类型 编号 vit次数 pit次数 ait 次数 pit 1001 2 1 1
vit 1001 2 1 1
vit 1001 2 1 1
ait 1001 2 1 1
vit 1002 1 0 0
insert into tb values('pit' , '1001')
insert into tb values('vit' , '1001')
insert into tb values('vit' , '1001')
insert into tb values('ait' , '1001')
insert into tb values('vit' , '1002')
create table tb(type varchar(10) , no varchar(10))
insert into tb values('pit' , '1001')
insert into tb values('vit' , '1001')
insert into tb values('vit' , '1001')
insert into tb values('ait' , '1001')
insert into tb values('vit' , '1002')
我已经给你写了那么多列子了,你之前提的
客户需求变来变去,我都快疯了。我发誓要好好学sql这次涨经验了,还要多谢谢你。。
http://www.cnblogs.com/insus/articles/1971446.html
SELECT
[type] ,[NO],
pit=SUM( CASE WHEN [type]='pit' THEN num ELSE 0 END),
ait= SUM( CASE WHEN [type]='ait' THEN num ELSE 0 END),
vit= SUM( CASE WHEN [type]='vit' THEN num ELSE 0 END)
FROM #temp GROUP BY [type] ,[NO]
ifnull((select count(1) from tb where type='vit' and tb.no = a.no group by tb.type),0) as vit ,
ifnull((select count(1) from tb where type='pit' and tb.no = a.no group by tb.type),0) as pit ,
ifnull((select count(1) from tb where type='ait' and tb.no = a.no group by tb.type),0) as ait
from tb a
ifnull((select count(1) from tb where type='vit' and tb.no = a.no group by tb.type),0) as vit ,
ifnull((select count(1) from tb where type='pit' and tb.no = a.no group by tb.type),0) as pit ,
ifnull((select count(1) from tb where type='ait' and tb.no = a.no group by tb.type),0) as ait
from tb a
谢谢tashiwoweiyi兄!不胜感激!!