我的购物车收入似乎每周都有所不同,星期一和星期四是低天,星期三和星期六是高天。因此,我想显示一个显示如下总数的网页:
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
答案 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
...
答案 1 :(得分: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(),interval -weekday(now())),但是我必须在那个部分考虑更多。