简体   繁体   English

查询以获取每个客户的每周平均支出

[英]Query to get weekly average spending each customer

i'm have question.. i have database mostly like this, how can i get weekly average of spendings from each customer? 我有一个问题..我的数据库大多是这样的,我怎样才能获得每个客户每周的平均支出? can you show me the sql query to this? 你能告诉我这个SQL查询吗?

here the example 这里的例子

| customer   | spending | date                |
| -----------|----------|---------------------|
| customer A |      105 | 2016-03-02 11:24:38 |
| customer A |       75 | 2016-02-02 11:24:38 |
| customer B |       35 | 2016-03-02 11:24:38 |
| customer A |       40 | 2016-03-02 11:24:38 |
| customer B |      105 | 2016-02-15 11:24:38 |
| customer A |      100 | 2016-03-02 11:24:38 |
| customer A |      150 | 2016-02-21 11:24:38 |
| customer A |      139 | 2016-03-02 11:24:38 |

and more... 和更多...

anyone can help me? 有人可以帮助我吗? thanks 谢谢

Try using a GROUP BY on the combination of customer and week of the year: 尝试结合使用客户和一年中的一周中的GROUP BY

SELECT customer, WEEK(date), AVG(spending)
FROM sales
GROUP BY customer, WEEK(date)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM