在sql分类汇总语句上,加一个子汇总统计

david-sui 2011-11-14 10:16:04
原sql:
select count (*) as number,paramvalue.param_value from paramvalue join project_fj on danjia between cast(left(param_value ,charindex('-',param_value )-1) as int) and cast(right(param_value ,len(param_value )-charindex('-',param_value )) as int) where paramvalue.paramclass_id=12 group by paramvalue.param_value

得到的结果:
param_value number
0-60 25
60-80 12

现在我要加一个子汇总count,他在原来number列的基础上,还有自己的子条件strWhere,统计数值count肯定比number小

想得到的结果:

param_value number count
0-60 25 8
60-80 12 10

求高手指点!

...全文
250 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2011-11-14
  • 打赏
  • 举报
回复
sum(case when 条件 then 1 else 0 end) as cnt
pengxuan 2011-11-14
  • 打赏
  • 举报
回复

select count (*) as number,paramvalue.param_value,cnt=sum(case when 条件 then 1 else 0 end) from paramvalue join project_fj on danjia between cast(left(param_value ,charindex('-',param_value )-1) as int) and cast(right(param_value ,len(param_value )-charindex('-',param_value )) as int) where paramvalue.paramclass_id=12 group by paramvalue.param_value
中国风 2011-11-14
  • 打赏
  • 举报
回复
或用

count (CASE WHEN 條件 THEN 列 end) as [count]
AcHerat 元老 2011-11-14
  • 打赏
  • 举报
回复
select ...,sum(case when 你的条件 then 1 else 0 end) [count]
from ...
黄_瓜 2011-11-14
  • 打赏
  • 举报
回复
sum(case when.... then 1 else 0 end)

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧