try this,with #t as
(select 'gao_ps' ss,'192.168.0.1' ip,1 is_reg,1 has_code union all
select 'gao_ps','192.168.0.1',1,1 union all
select 'gao_ps','192.168.1.1',1,1 union all
select 'gao_ps','192.168.1.1',0,1
)
select ss,
count(1) '注册个数',
sum(case when has_code=1 then 1 else 0 end) '弹代码数',
count(distinct ip) '重复IP注册数'
from #t
where is_reg=1
group by ss
/*
ss 注册个数 弹代码数 重复IP注册数
------ ----------- ----------- -----------
gao_ps 3 3 2(1 row(s) affected)
*/
(select 'gao_ps' ss,'192.168.0.1' ip,1 is_reg,1 has_code union all
select 'gao_ps','192.168.0.1',1,1 union all
select 'gao_ps','192.168.1.1',1,1 union all
select 'gao_ps','192.168.1.1',0,1
)
select ss,
count(1) '注册个数',
sum(case when has_code=1 then 1 else 0 end) '弹代码数',
count(distinct ip) '重复IP注册数'
from #t
where is_reg=1
group by ss
/*
ss 注册个数 弹代码数 重复IP注册数
------ ----------- ----------- -----------
gao_ps 3 3 2(1 row(s) affected)
*/
解决方案 »
- sql 基础
- 一个比较难的sql语句,各位高手帮帮忙?
- 如何在SQLSERVER中使用SQL语句在ACCESS MDB中创建表?
- distinct和count联合使用的方法?
- 怎么使用游标
- 主表和子表做关联,(一条主表id对应多条子表信息),当我有n条主表id时候,我如何分别得到各个子表的第一条数据呢
- 日期差值,急
- 【求助】如何把查询出来的结果保存到临时表?
- 一个问题,word或xls的数据可以导入到sqlserver里吗?
- 各位大侠,请问在sql server2000中执行"SELECT * INTO [;DATABASE=c:\db2.mdb].BK_T1 FROM T1"语句会出错?
- TDS 协议流无效,请教解决方法
- 关于不才用列名查询的问题
应该是单ip注册个数大于等于2的注册个数总和.二者好像不太一致喔.