我有一个表“订单”,其中的列看起来像这样:
| order_id | shop_id | order_total | currency | created_at |
现在我想做一个查询,它返回一个如下所示的表:
| interval | currency | sum |
其中interval 是日期间隔,currency 是货币,sum 是该时间间隔内所有订单的总和。一个例子可以是:
| 2012-08-12-2012-09-12 | EUR | 540922 |
| 2012-08-12-2012-09-12 | DKK | 43215 |
| 2012-09-12-2012-10-12 | EUR | 123643 |
| 2012-09-12-2012-10-12 | DKK | 0 |
我已经有一个不考虑货币的查询,但我不知道如何让它考虑货币..有什么想法吗?
这是我现在使用的查询示例。真正的查询当然是由代码生成的,这只是一个例子。
SELECT CASE
WHEN created_at BETWEEN '2012-08-12' AND '2012-09-12' THEN '2012-08-12-2012-09-12'
WHEN created_at BETWEEN '2012-09-12' AND '2012-10-12' THEN '2012-09-12-2012-10-12'
WHEN created_at BETWEEN '2012-10-12' AND '2012-10-14' THEN '2012-10-12-2012-10-14'
end AS intrvl,
Sum(order_total)
FROM `order` o
WHERE shop_id = 4
AND created_at BETWEEN '2012-08-12' AND '2012-11-17'
GROUP BY CASE
WHEN created_at BETWEEN '2012-08-12' AND '2012-09-12' THEN '2012-08-12-2012-09-12'
WHEN created_at BETWEEN '2012-09-12' AND '2012-10-12' THEN '2012-09-12-2012-10-12'
WHEN created_at BETWEEN '2012-10-12' AND '2012-10-14' THEN '2012-10-12-2012-10-14'
end
/莫腾