日期:2014-05-16 浏览次数:20552 次
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2014-03-03 16:30:06
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([日期] datetime,[胜负] varchar(2))
insert [huang]
select '2012/3/31','胜' union all
select '2012/3/31','负' union all
select '2012/3/31','胜' union all
select '2012/3/30','胜' union all
select '2012/3/30','负' union all
select '2012/3/30','负'
--------------开始查询--------------------------
select [日期],COUNT(CASE WHEN [胜负]='胜' THEN 1 ELSE NULL END ) '胜',
COUNT(CASE WHEN [胜负]='负' THEN 1 ELSE NULL END ) '负',
COUNT([胜负])'合计'
from [huang]
GROUP BY [日期]
----------------结果----------------------------
/*
日期 胜 负 合计
----------------------- ----------- ----------- -----------
2012-03-30 00:00:00.000 1 2 3
2012-03-31 00:00:00.000 2 1 3
*/