当前位置: 代码迷 >> ASP.NET >> sql
  详细解决方案

sql

热度:2447   发布时间:2013-02-26 00:00:00.0
求一个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
  相关解决方案