2015-11-19 124 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]);