导航
  • 主页
  • 基础类
  • 应用实例
  • 新技术前沿

sql语句 能否区分一列的值,进行统计记录条数

andrew_zx 2007-12-19 09:05:22
select calling_device as cd,dnis_area as da, answer_duration as ad,count(*) as cnt from call_master group by calling_device,dnis_area,answer_duration

得出第三列的值的意义在于区分大于0和等于0,数字并无意义;第四列cnt为合计列

cd da as cnt

6003 2 0 1
6006 3 7898 1
8001 0 0 1
8001 0 11 1
8001 0 32 1
8001 0 33 1
8001 0 36 1
8001 0 38 2
8001 2 0 1
8001 3 432 1
8001 3 456 1
8001 3 8789 1
8001 4 343 1
8001 4 879 1
8002 0 145 1

能否区分第三列 区分为=0 和>0 第四列则变成合计分组前两列后 根据第三列=0或是>0计算合计多少条记录

即 sql结果变成

cd da as cnt

6003 2 =0 1
6006 3 >0 1
8001 0 =0 1
8001 0 >0 6
8001 2 =0 1
8001 3 >0 3
8001 4 >0 2
8002 0 >0 1



...全文
165 点赞 收藏 4
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
andrew_zx 2007-12-20
佩服
回复
hanjoe109 2007-12-20
高手!
回复
pt1314917 2007-12-19

上面敲错了。不好意思:
应该是
select
cd,da,case when ad>0 then '>0' when ad=0 then '=0' end ad,
sum(cnt) cnt
from
(
select calling_device as cd,dnis_area as da, answer_duration as ad,count(*) as cnt from call_master group by calling_device,dnis_area,answer_duration
)a

group by cd,da,case when ad>0 then '>0' when ad=0 then '=0' end
回复
pt1314917 2007-12-19


select
cd,da,case when ad>0 then '>0' when ad=0 then '=0' end ad,
sum(cnt) cnt
from
(
select calling_device as cd,dnis_area as da, answer_duration as ad,count(*) as cnt from call_master group by calling_device,dnis_area,answer_duration
)a
group by cd,da,ad
回复
发动态
发帖子
MS-SQL Server
创建于2007-09-28

3.2w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
社区公告
暂无公告