你只需要通過該表的年份和月份日期零部件集團獲得每月和每年數:
select
count(*), datePart("yy", requestDate) + "/" + datePart("mm", requestDate)
from table1
group by
datePart("yy", requestDate), datePart("mm", requestDate)
要得到這些的總和,你就必須有一個臨時表和然後用運行總數更新該臨時表總和列。
create table #temp (rowID identity(1,1) int, dateCount int, yearMonth varchar(50), runningTotal int)
insert into #temp (dateCount, yearMonth, runningTotal)
select
count(*), datePart("yy", requestDate) + "/" + datePart("mm", requestDate)
from table1
group by
datePart("yy", requestDate), datePart("mm", requestDate)
update #temp set runningTotal = (select sum(dateCount) from #temp a where a.rowID < #temp.rowID)
select * from #temp
drop table #temp
你的意思是T-SQL? 'RequestDate'是一個'varchar'或'datetime'? – 2010-05-19 14:08:35
你在總結什麼? – 2010-05-19 14:09:28
RequestDate是日期時間和我正在求和D_Count – Ashish 2010-05-19 14:11:58