SQL查询报告

时间:2017-09-27 21:42:25

标签: mysql database

我刚开始学习MySQL。我想运行一个查询,每周给我一个销售量。

到目前为止,我只有:

SELECT sku,SUM(qty_ordered) FROM sales_flat_order_item
WHERE created_at BETWEEN NOW()-INTERVAL 122 WEEK AND NOW()
GROUP BY sku

1 个答案:

答案 0 :(得分:0)

您还需要GROUP BY WEEK()

SELECT sku, YEAR(created_at), WEEK(created_at) as week_no, SUM(qty_ordered)
FROM sales_flat_order_item
WHERE created_at BETWEEN NOW()-INTERVAL 122 WEEK AND NOW()
GROUP BY sku, YEAR(created_at), WEEK(created_at)

或者如果你想要第一个和sku:

SELECT YEAR(created_at) as year_created, WEEK(created_at) as week_no, sku, SUM(qty_ordered)
FROM sales_flat_order_item
WHERE created_at BETWEEN NOW()-INTERVAL 122 WEEK AND NOW()
GROUP BY YEAR(created_at), WEEK(created_at), sku