日期:2014-05-17 浏览次数:20559 次
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-09-24 13:15:19
-- Version:
-- Microsoft SQL Server 2014 (CTP1) - 11.0.9120.5 (X64)
-- Jun 10 2013 20:09:10
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([cName] varchar(8),[status] varchar(4))
insert [huang]
select '博爱店','离线' union all
select '博爱店','离线' union all
select '博爱店','离线' union all
select '博爱店','离线' union all
select '石峰店','在线' union all
select '石峰店','离线' union all
select '石峰店','在线' union all
select '奥体店','离线' union all
select '奥体店','离线' union all
select '八里街店','离线' union all
select '八里街店','在线' union all
select '八里街店','在线' union all
select '八里街店','在线' union all
select '八里街店','离线' union all
select '宝安店','离线' union ALL
select '宝安店','在线' union all
select '宝安店','在线'
--------------开始查询--------------------------
select cname,COUNT(CASE WHEN [status] ='离线' THEN 0 ELSE 1 END )
from [huang]
GROUP BY cname
----------------结果----------------------------
/*
cname
-------- -----------
奥体店 2
八里街店 5
宝安店 3
博爱店 4
石峰店 3
*/
select cname,sum(CASE WHEN [status] ='离线' THEN 0 ELSE 1 END ) from [huang] GROUP BY cname