我使用以下代码按月显示收入。我想使用类似的数据,然后按周(数字)分组。
我的问题:除了修改下面的查询之外,还有更优雅的方法来制作更短的查询吗?我是否必须使用52个子查询按周计算收入,而不是按月计算12个子查询?
SELECT
IfNull(Year,'Totals') Year,
`Jan`,`Feb`,`Mar`,`Apr`,`May`,`Jun`,`Jul`,`Aug`,`Sep`,`Oct`,`Nov`,`Dec`,Qty AS Totaal
FROM (
SELECT
year(orders.Order_date) AS 'Year',
sum(if(month(Order_date)= 1, (order_create.amount * order_create.prices), 0)) as Jan,
sum(if(month(Order_date)= 2, (order_create.amount * order_create.prices), 0)) as Feb,
sum(if(month(Order_date)= 3, (order_create.amount * order_create.prices), 0)) as Mar,
sum(if(month(Order_date)= 4, (order_create.amount * order_create.prices), 0)) as Apr,
sum(if(month(Order_date)= 5, (order_create.amount * order_create.prices), 0)) as May,
sum(if(month(Order_date)= 6, (order_create.amount * order_create.prices), 0)) as Jun,
sum(if(month(Order_date)= 7, (order_create.amount * order_create.prices), 0)) as Jul,
sum(if(month(Order_date)= 8, (order_create.amount * order_create.prices), 0)) as Aug,
sum(if(month(Order_date)= 9, (order_create.amount * order_create.prices), 0)) as Sep,
sum(if(month(Order_date)=10, (order_create.amount * order_create.prices), 0)) as Oct,
sum(if(month(Order_date)=11, (order_create.amount * order_create.prices), 0)) as Nov,
sum(if(month(Order_date)=12, (order_create.amount * order_create.prices), 0)) as `Dec`,
ROUND(SUM(order_create.amount * order_create.prices))),2) AS Qty
FROM
order_create
INNER JOIN orders ON (order_create.order_ID = orders.order_ID)
GROUP BY year
) sums ;