0

I am using sql server 2008 R2. I am having a table in database that contains data regarding patients that are of different status like Active, Completed, Inactive etc. I am asked to get the patients with "Active" status group by date.

I have created following query :

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)

and that gives me the result like below :

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

But, It is giving me the number of active patients on specific date. I want the sum of total active patients upto that specific date.

Any solution?

Thanks

4

2 回答 2

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

根据 usingOVER子句的附加信息可以在这里找到:http ://technet.microsoft.com/en-us/library/ms189461.aspx

于 2013-11-05T15:57:59.467 回答
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
于 2013-04-09T14:19:13.623 回答