2012-09-10 104 views
3

在我的SqlServer 2008中,我有一個名爲Table1的表,其中一列名爲IncDate這是一個DateTime。基於日期的記錄總數記錄

我需要運行一個查詢,該查詢返回該日期之前的記錄總數。例如,我的表中有這些記錄:

IncDate 
09/09/2012 16:21:51.547 
09/09/2012 16:22:11.032 
10/09/2012 13:51:33.327 

我的查詢需要的輸出:

Date................Total 
09/09/2012...........2 
10/09/2012...........1 

回答

0

這裏有一種方法:

select cast(incdate as date) as date, count(*) 
from table1 
group by cast(incdate as date) 
order by 1 
3

您需要CAST()的日期與日期只要。

SELECT Cast(incdate as date) DateOnly, Count(*) AS Total 
FROM yourtable 
GROUP BY Cast(incdate as date) 

看到SQL Fiddle with Demo

如果不這樣做CAST()日至今只,那麼計數將被關閉。

0

嘗試

SELECT cast(incdate as [date]) as [idate], count(incdate) totalCount 
FROM table1 
GROUP BY cast(incdate as [date]) 
ORDER BY totalCount