2015-11-19 98 views
0

我有一個訂單的詳細信息表,我想每個日期找到價格的總和,並顯示它們作爲如何分組訂單按日期

對於防爆

count sum   date 
    5  619.95  2015-11-19 
    3  334.97  2015-11-18 
    4  734.96  2015-11-18 
    5  1129.95  2015-11-18 

我寫的查詢得到countsum作爲

select count(id), sum([price]) 
from [OrderDetails] 
where [date]between '2015-10-29 05:15:00' and '2015-11-09 00:01:00' 
group by datepart(day,[date]) 

但是不能夠實現與日期。如何做呢?

回答

1

好像你列稱爲date既有日期及時間分量。我建議將其轉換爲一個日期,同時爲selectgroup by

select count(id), sum(price), cast([date] as date) as thedate 
from OrderDetails 
where [date] between '2015-10-29 05:15:00' and '2015-11-09 00:01:00' 
group by cast([date] as date) 
order by thedate; 

注:date是一列一個可憐的名字,因爲它是一個內置式的名稱。

+0

@DheerajPatnaik。 。 。有沒有理由不接受這個答案? –

3

您需要包括你在查詢的SELECT部分上進行分組:

select count(id), sum([price]), datepart(day,[date]) as [date] 
from [OrderDetails] 
where [date] between '2015-10-29 05:15:00' and '2015-11-09 00:01:00' 
group by datepart(day,[date]);