简体   繁体   English

选择此选项可按客户将首次销售考虑在内的方式计算客户的平均销售收入

[英]Select to Calculate Sales Average by Customer taking First Sales into Account MYSQL

I have this Sales Table by Customer in Mysql 我在MySQL中有按客户分类的销售表

+-----------+------------+-------+-----------------+
| Customer  |    Date    | Sales | Date_First_Sale |
+-----------+------------+-------+-----------------+
| Jane      | 2016-04-30 |   903 | 2015-02-03      |
| Jane      | 2016-02-03 |    51 | 2015-02-03      |
| Jane      | 2016-03-09 |   192 | 2015-02-03      |
| John      | 2016-05-10 |    64 | 2015-10-03      |
| John      | 2016-04-16 |   880 | 2015-10-03      |
| John      | 2016-08-17 |   386 | 2015-10-03      |
| John      | 2016-03-01 |   503 | 2015-10-03      |
| Juan      | 2016-07-06 |   765 | 2015-09-01      |
| Juan      | 2016-01-20 |    36 | 2015-09-01      |
| Juan      | 2016-03-03 |   928 | 2015-09-01      |
| Momo      | 2016-06-29 |   573 | 2015-09-01      |
| Momo      | 2016-04-25 |   375 | 2015-09-01      |
| Momo      | 2016-06-10 |   999 | 2015-09-01      |
| Nour      | 2016-02-28 |   956 | 2015-05-01      |
| Nour      | 2016-01-03 |   582 | 2015-05-01      |
| Nour      | 2016-08-17 |   366 | 2015-05-01      |
| Philip    | 2016-03-22 |   296 | 2015-09-01      |
| Philip    | 2016-04-14 |   459 | 2015-09-01      |
| Sylvie    | 2016-03-29 |   551 | 2015-09-03      |
| Sylvie    | 2016-02-14 |   896 | 2015-09-03      |
+-----------+------------+-------+-----------------+

I need to calculate the Average Sales by Customer calculated on a WEEKLY basis in the last 12 months (52 or 53 weeks depending on the calendar?), starting from Today. 从今天开始,我需要计算过去12个月(取决于日历是52周还是53周?)中每周一次的客户平均销售额。

Now the problem is that I do not want to calculate the Average Weekly sales by customer for customers that have made their first purchase in a range below 12 months, for instance If current date is 2016-09-01, and Customers made his first purchase on 2016-07-24, the average should not be calculated on a 12 months basis but on the weekly sales generated between the 2016-07-24 and the 2016-09-01 only. 现在的问题是,我不想为在12个月以下的范围内进行首次购买的客户计算客户的平均每周销售额,例如,如果当前日期为2016-09-01,并且客户进行了首次购买在2016年7月24日,平均值不应以12个月为基础计算,而应仅基于2016年7月24日至2016年9月1日之间产生的每周销售额。

For customers who have made their First purchase before the 12 months range, then the average should be calculated on 12 months only. 对于在12个月范围内进行首次购买的客户,则平均值应仅按12个月计算。

I have been trying to find this SELECT but have not reached anything due to my limited Mysql knowledge for more complex queries! 我一直在尝试找到此SELECT,但由于对更复杂的查询的Mysql知识有限,因此未找到任何内容!

Thanks in advance for your help 在此先感谢您的帮助

This should help you 这应该对你有帮助

SELECT Customer, (total_sales/weeks) AS avg_sales FROM
(
SELECT Customer, total_sales, Date_First_Sale, IF(weeks>52,52,weeks) as weeks
FROM (
    SELECT Customer, SUM(Sales) AS total_sales, Date_First_Sale, TIMESTAMPDIFF(WEEK, Date_First_Sale, CURDATE()) AS weeks
    FROM (
        SELECT Customer, sales , Date_First_Sale
        FROM test.SO_customer
        WHERE Date > DATE_SUB(curdate(), INTERVAL 1 YEAR)
         ) as subTable
    GROUP BY Customer
    ) as subTable2
) as subTable3

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

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