当前位置: 代码迷 >> Sql Server >> 多项同时统计的有关问题
  详细解决方案

多项同时统计的有关问题

热度:61   发布时间:2016-04-27 16:44:51.0
多项同时统计的问题?
select   count(字段)   as   js1,count(字段)   as   js2   中
count(字段)   as   js1和count(字段)   as   js2   统计条件不同,这样的sql语句怎么写?

------解决方案--------------------
--try

select
sum(case when 字段= '值1 ' then 1 else 0 end) as js1,
sum(case when 字段= '值2 ' then 1 else 0 end) as js2,
from tbName

------解决方案--------------------
select B.名称, count(*) sj,
sum(case when 字段= '值1 ' then 1 else 0 end) as js1,
sum(case when 字段= '值2 ' then 1 else 0 end) as js2
from A inner join B on A.代码=B.代码 and B.类别= '水果 '
group by B.名称
  相关解决方案