日期:2014-05-17 浏览次数:20737 次
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-09-26 13:16:13
-- 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]([taskid] int,[iscall] varchar(5),[ismsg] varchar(5),[isfax] varchar(5),[isemail] varchar(5))
insert [huang]
select 12,'false','false','false','false' union all
select 12,'false','false','false','false' union all
select 12,'true','false','false','false' union all
select 23,'false','false','false','false' union all
select 23,'false','false','false','false' union all
select 23,'true','false','false','false'
--------------开始查询--------------------------
select [taskid] ,CONVERT(VARCHAR(2),sum(CASE WHEN [iscall]='true' THEN 1 ELSE 0 END ))+'/'+CONVERT(VARCHAR(2),COUNT(iscall))[iscall],
CONVERT(VARCHAR(2),sum(CASE WHEN [ismsg]='true' THEN 1 ELSE 0 END ))+'/'+CONVERT(VARCHAR(2),COUNT([ismsg]))[ismsg],
CONVERT(VARCHAR(2),sum(CASE WHEN [isfax]='true' THEN 1 ELSE 0 END ))+'/'+CONVERT(VARCHAR(2),COUNT([isfax]))[isfax],
CONVERT(VARCHAR(2),sum(CASE WHEN [isemail]='true' THEN 1 ELSE 0 END ))+'/'+CONVERT(VARCHAR(2),COUNT([isemail]))[isemail]
from [huang]
GROUP BY [taskid]
----------------结果----------------------------
/*
taskid iscall ismsg isfax isemail
----------- ------ ----- ----- -------
12