日期:2014-05-17  浏览次数:20383 次

关于存储过程方面的问题
  

  



  例如上图的表Test。 

  吐过是同一个人的记录,那么AJBH,AJNBBH是相同的。

  现在表中有三个人的六条记录。

  后三条记录是同一个人的。



  我在一个存储过程里,想统计出表中有几个人的记录。

  请问,统计条件应该怎么写啊?


 
sum(case when .... then 1 else 0 end) as Data9


 when后面应该怎么写?

------解决方案--------------------
----------------------------------------------------------------
-- 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
*/