计数记录总和

时间:2013-04-09 12:15:22

标签: sql-server sql-server-2008 sql-server-2005 sql-server-2008-r2

我使用的是sql server 2008 R2。我在数据库中有一个表格,其中包含有关不同状态的患者的数据,如活动,完成,非活动等。我被要求让患者使用" Active"状态组按日期。

我创建了以下查询:

SELECT COUNT(*) AS TotalActivePatients, CONVERT(VARCHAR(10), CreatedDtUTC,111) AS CreatedDtUTC
FROM TablePatient 
WHERE MonitoringStatus LIKE 'Active' 
AND IsDeleted=0
GROUP BY CONVERT(VARCHAR(10), CreatedDtUTC,111)

这给我的结果如下:

TotalActivePatients CreatedDtUTC
1                   2013/02/24
2                   2013/02/25
4                   2013/02/28
4                   2013/03/01
1                   2013/03/06
1                   2013/03/10
3                   2013/03/11
2                   2013/03/14
3                   2013/03/15
2                   2013/03/16

但是,它给了我特定日期活跃患者的数量。我想要总活跃患者的总和达到特定日期。

任何解决方案?

由于

2 个答案:

答案 0 :(得分:0)

这将为您提供总计......

SELECT  DISTINCT COUNT(1) OVER (ORDER BY CONVERT(VARCHAR(10), CreatedDtUTC,111)), 
        CONVERT(VARCHAR(10), CreatedDtUTC,111) AS CreatedDtUTC
FROM    TablePatient 
WHERE   MonitoringStatus = 'Active' 
AND     IsDeleted = 0

答案 1 :(得分:0)

正确答案如下:

SELECT  DISTINCT
     CONVERT(VARCHAR(10), CreatedDtUTC,111) AS CreatedDtUTC,
     COUNT(1) OVER (PARTITION BY CONVERT(VARCHAR(10), CreatedDtUTC,111)), 

FROM    TablePatient 
WHERE   MonitoringStatus = 'Active' 
AND     IsDeleted = 0
ORDER BY 1

根据使用OVER子句的其他信息,请访问:http://technet.microsoft.com/en-us/library/ms189461.aspx