我的購物車收入似乎因星期幾而異,週一和週四爲低日,週三和週六爲高日。因此,我想顯示網頁顯示總計這樣的:MySQL在一週中的日總計
Week # Sun Mon Tue Wed Thu Fri Sat
Week 1 $5.00 $1.00 $3.00 $9.00 $1.00 $3.00 $9.00
Week 2 $5.23 $1.07 $2.98 $8.75 $0.02 $3.14 $7.51
Week 3 etc.
我可以查詢一週的像這樣的一天:
SELECT count(id) AS orders,
order_date,
date_format(order_date, '%a') AS weekday,
sum(total) AS revenue
FROM `ss_orders`
WHERE dayofweek(order_date) = 1
AND order_date >= date_add(now() , INTERVAL -83 DAY)
GROUP BY order_date
ORDER BY order_date DESC
這給了我每天的總量爲所有星期日在過去的12周。因此,我可以做7個查詢來獲得我需要的信息(每週的每一天查詢1次)。我似乎應該能夠在單個查詢中獲得整個事情。
查詢應該是什麼?謝謝!
編輯:這是從首選解決方案更正的查詢。
SELECT
week(o.order_date) as WkNumber,
sum(if(weekday(o.order_date) = 6, 1, 0) * o.total) as SalesSun,
sum(if(weekday(o.order_date) = 6, 1, 0)) as OrdersSun,
sum(if(weekday(o.order_date) = 0, 1, 0) * o.total) as SalesMon,
sum(if(weekday(o.order_date) = 0, 1, 0)) as OrdersMon,
sum(if(weekday(o.order_date) = 1, 1, 0) * o.total) as SalesTue,
sum(if(weekday(o.order_date) = 1, 1, 0)) as OrdersTue,
sum(if(weekday(o.order_date) = 2, 1, 0) * o.total) as SalesWed,
sum(if(weekday(o.order_date) = 2, 1, 0)) as OrdersWed,
sum(if(weekday(o.order_date) = 3, 1, 0) * o.total) as SalesThu,
sum(if(weekday(o.order_date) = 3, 1, 0)) as OrdersThu,
sum(if(weekday(o.order_date) = 4, 1, 0) * o.total) as SalesFri,
sum(if(weekday(o.order_date) = 4, 1, 0)) as OrdersFri,
sum(if(weekday(o.order_date) = 5, 1, 0) * o.total) as SalesSat,
sum(if(weekday(o.order_date) = 5, 1, 0)) as OrdersSat,
sum(o.total) as SalesWeek,
sum(1) as OrdersWeek
from
ss_orders o
where
o.order_date > date_add(now(), INTERVAL -13 WEEK)
group by
week(o.order_date)
order by o.order_date desc
我將編輯過的查詢添加到問題中。謝謝! –