日期:2014-05-19  浏览次数:20449 次

求一个sql
id     souce     channelid
1         0               1
2         0               1
3         1               1
4         1               3
5         0               3
6         1               3
......
得到的结果是   当   souce=0的时候
zs     souce     channelid
2           0           1
1           0         3

------解决方案--------------------
select count(1) as zs, souce, channelid
from tb
where souce=0
group by souce,channelid
------解决方案--------------------
select count(*) as zs ,souce,channelid
from tablename
where souce=0
group by souce,channelid
------解决方案--------------------
create table t(id int, souce int , channelid int )
insert t
select 1 , 0 , 1
union all select 2 , 0 , 1
union all select 3 , 1 , 1
union all select 4 , 1 , 3
union all select 5 , 0 , 3
union all select 6 , 1, 3


select zs=count(souce),souce,channelid
from t
where souce = 0
group by souce,channelid

drop table t
------解决方案--------------------
/* create table table1(
id int ,
source int,
channelid int) */

--select * from table1
1 0 1
2 0 1
3 1 1
4 1 3
5 0 3
6 1 3
insert into table1 values(6 , 1 , 3)

select count(source) as zs,source,channelid from table1 group by source,channelid order by source
------解决方案--------------------
select zs=count(1),0 as souce,channelid
from t
where souce = 0
group by channelid