简体   繁体   English

MYSQL查询每周获取新客户

[英]MYSQL query to get new customers each week

I'm struggling with how I can get a report of new customers each week (those who have never ordered previously). 我正在为每周如何获得新客户(以前从未订购过)的报告而苦苦挣扎。 The results should look as follows: 结果应如下所示:

WEEK_ENDING_DATE   NEW_CUSTOMERS
----------------   -------------
2019-02-03         50
2019-02-10         60

My query does a Right Outer Join of old_orders and new_orders to find new customers (see query below). 我的查询对old_orders和new_orders进行了右外部联接以查找新客户(请参见下面的查询)。

I also have a helper table called my_calendars that helps me to group by week_end_date. 我还有一个名为my_calendars的帮助程序表,可以帮助我按week_end_date进行分组。 The my_calendars table has rows containing each date in the year, the corresponding week_begin_date and week_end_date for that date. my_calendars表中的行包含一年中的每个日期,该日期的相应week_begin_date和week_end_date。 For example, for a date like 2019-02-15, the week_begin_date is 2019-02-11 and week_end_date is 2019-02-17 (Week is Mon - Sun, Format = YYYY-MM-DD). 例如,对于像2019-02-15这样的日期,week_begin_date是2019-02-11,week_end_date是2019-02-17(Week是Mon-Sun,格式= YYYY-MM-DD)。 The helper table looks as follows: 帮助器表如下所示:

DATE           WEEK_BEGIN_DATE      WEEK_END_DATE
----------     ----------------     -------------
2019-02-15     2019-02-11           2019-02-17
2019-01-08     2019-01-07           2019-01-13

Now, back to my query. 现在,回到我的查询。 I want to be able to find new customers each week. 我希望每周都能找到新客户。 The problem I'm having is that I can't figure out how to place each week of the year in the query so as to compare the order dates. 我遇到的问题是我无法弄清楚如何将一年中的每个星期放在查询中以便比较订单日期。 The old_orders are orders that took place before 'this week', and the new_orders are those that took place 'this week'. 旧订单是 “本周” 之前发生的订单,新订单是 “本周”发生的订单。 The query works fine when I use static dates, but I'm struggling to make the dates variable ie each week in the year. 当我使用静态日期时,查询工作正常,但是我正在努力使日期可变,即一年中的每个星期。 See my questions in the query where I'm having challenges. 在有挑战的查询中查看我的问题。

SELECT 
    new_orders.week_end_date
    ,COUNT(DISTINCT new_orders.customer) AS new_customers       
FROM
      (SELECT * 
        FROM orders old
        INNER JOIN my_calendar cal ON cal.date = old.order_date
        #The line below works, but it's a static date of Feb 4. How do I replace it with each week in the calendar
        WHERE cal.week_end_date < '2019-02-04'
        #The commented line below does not work
        #WHERE cal.date < cal.week_end_date
      ) AS old_orders
RIGHT OUTER JOIN (SELECT * 
                  FROM order_items_view new
                  INNER JOIN my_calendar cal ON cal.date = new.order_date
                  #How do I replace the static dates below and compare with each week in the calendar  
                  WHERE cal.date BETWEEN '2019-02-04' and '2019-02-10'
                  #The commented line below didn't work
                  #WHERE cal.week_end_date = cal.week_end_date
                 ) AS new_orders
ON new_orders.customer = old_orders.customer
WHERE old_orders.customer IS NULL
GROUP BY new_orders.week_end_date

I would first use an aggregated subquery to compute the first order date of each customer, and then join the result with the calendar table : 我将首先使用聚合子查询来计算每个客户的第一笔订单日期,然后将结果与日历表结合起来:

SELECT
    c.week_end_date,
    COUNT(o.customer) AS new_customers
FROM
    my_calendar AS c
    LEFT JOIN (
        SELECT customer, MIN(order_date) first_order_date
        FROM orders
        GROUP BY customer
    ) AS o ON c.date = o.first_order_date
GROUP BY c.week_end_date

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

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