日期:2014-05-17 浏览次数:20468 次
sum(case when .... then 1 else 0 end) as Data9
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-12-09 10:26:22
-- 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: )
--
----------------------------------------------------------------
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
go
create table [test]([id] int,[ajbh] int,[ajnbbh] varchar(5),[sqsj] datetime,[sjsj] datetime,[sfjl] int)
insert [test]
select 1,111,'00111','20131209','20131209',1 union all
select 2,222,'00333','20131209','20131209',1 union all
select 3,333,'00222','20131209','20131209',1 union all
select 4,333,'00222','20131208','20131208',0 union all
select 5,333,'00222','20131207','20131207',1 union all
select 6,333,'00222','20131206','20131206',0
--------------开始查询--------------------------
SELECT COUNT(1)
FROM (
select COUNT(1) [COUNT]
from [test]
GROUP BY [ajbh],[ajnbbh])a
----------------结果----------------------------
/*
-----------
3
*/