mysql查每天的销售额,计算MySQL每天的平均销售额

In my database I have a table with two columns. The first column contains dates and the second is a count variable. I was wondering if it is possible to compute the average counts for each weekday based on the dates and counts.

In the following a small example:

Table:

Date Count

02/01/2005 100

02/02/2005 200

02/03/2005 300

... ...

Output:

Days Average

Monday 120.5

Tuesday 200.2

Wednesday 300.5

解决方案

You could a series of avg calls on case expression extracting the day's name:

SELECT AVG(CASE DAYOFWEEK(`date`) WHEN 2 THEN `count` ELSE NULL END) AS Monday,

AVG(CASE DAYOFWEEK(`date`) WHEN 3 THEN `count` ELSE NULL END) AS Tuesday,

AVG(CASE DAYOFWEEK(`date`) WHEN 4 THEN `count` ELSE NULL END) AS Wednesday,

AVG(CASE DAYOFWEEK(`date`) WHEN 5 THEN `count` ELSE NULL END) AS Thursday,

AVG(CASE DAYOFWEEK(`date`) WHEN 6 THEN `count` ELSE NULL END) AS Friday

FROM mytable

EDIT:

Given the updated expected output in the edited post, it's much easier to do - just group by the dayname:

SELECT DAYNAME(`date`), AVG(`count`)

FROM mytable

WHERE DAYOFWEEK(`date`) BETWEEN 2 AND 6

GROUP BY DAYNAME(`date`)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值