如何按周汇总订单总金额

时间:2017-02-22 03:51:02

标签: mysql

我正在尝试检索最近3个月的记录。我需要按周汇总订单总额。我做了以下查询。

select CONCAT("Week", WEEK(created_at)) as week_number, sum(total_cost) 
from certified_diamond_orders 
where created_at > 2016-11-22 
and status = "delivered" 
group by week("created_at")

但我只收到一条记录。事实上,我的表有2年的条目。此外,我试图找出如何将周开始日期和结束日期拉到我的图表上。

我犯错误的任何建议?

1 个答案:

答案 0 :(得分:2)

week("created_at")看起来您正在尝试确定字符串"created_at"的周而不是列created_at。这可能解释了为什么您只在结果中获得一行。

日期2016-11-22看起来也很可疑,而不是日期(2016 - 11 - 22 = 1983 vs "2016-11-22"

试试这个:

SELECT
  CONCAT('Week', WEEK(created_at)) AS week_number,
  SUM(total_cost)
FROM certified_diamond_orders 
WHERE
  created_at > '2016-11-22' AND
  status = 'delivered'
GROUP BY WEEK(created_at)