繁体   English   中英

如何按日期分组订单

[英]How to group orders by Date

我有一个订单明细表,我想按每个日期查找价格总和,并将其显示为

对于前

   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])

但无法实现与日期。 如何做呢?

您需要在查询的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]);

您的名为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是列的较差名称,因为它是内置类型的名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM