当前位置: 代码迷 >> Sql Server >> 怎么实现count(TickState=2)这种样式
  详细解决方案

怎么实现count(TickState=2)这种样式

热度:62   发布时间:2016-04-27 20:14:37.0
如何实现count(TickState=2)这种样式?
select   count(TickState)   as   UseTotal,count(TickState=2)   as   WasteTotal
from   toll_list
就是一个字段有3种取值,取出每种取值的记录数.
但是不能用该字段group.因为还要取其它值.
谢谢!

------解决方案--------------------
select count(TickState) as UseTotal,sum(case when TickState=2 then 1 else 0 end) as WasteTotal
from toll_list
------解决方案--------------------
select UseTotal = sum(case TickState when 1 then 1 else 0 end),
WasteTotal = sum(case TickState when 2 then 1 else 0 end)
from toll_list
  相关解决方案