2013-02-06 58 views
1

我的購物車收入似乎因星期幾而異,週一和週四爲低日,週三和週六爲高日。因此,我想顯示網頁顯示總計這樣的: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 

回答

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 -12 WEEK) 
    group by 
     week(o.order_date) 

您可能必須調整WHERE子句真正獲得12個全周,如果你是在一個星期的中間......如NOW()改變爲類似 DATE_ADD(NOW(),間隔 - 周(現在())),但我不得不在這一方面考慮更多。

+0

我將編輯過的查詢添加到問題中。謝謝! –

2

按星期分組,然後一天?

SELECT count(id) AS orders, 
     date_format(order_date, '%a') AS weekday, 
     WEEK(order_date) AS week_number, 
     sum(total) AS revenue 
FROM 
     `ss_orders` 
WHERE 
     order_date >= date_add(now() , INTERVAL -83 DAY) 
GROUP BY 
     WEEK(orderdate), DAY(orderdata) 
ORDER BY 
     order_date DESC 

應該給你這樣的結果:

orders | weekday | week_number | revenue 
5  Sun  1    20 
6  Mon  1    15 
... 
+0

這兩個解決方案的工作,希望我可以採取兩個!但事實上,數據透視表就是我想要的。 –