简体   繁体   中英

Total orders by new customers Each day and week

I'm fairly new to SQL and I'm trying get total orders by new customers every day and each week in Mysql ( feel free to answer in any sql versions). First I tried to get total orders by new customers every day by writing below query, however Im getting incorrect results.

select COUNT(u.orderId), date(u.createdAt) as ord_dt, u.userId
from userorder u 
    inner join
        (
            select userId, min(date(createdAt)) as first_date
            from userorder 
            group by 1
        ) g 
        on g.userId =  u.userId
where g.first_date < date(u.createdAt)
group by 3

Link to the dataset https://docs.google.com/spreadsheets/d/1fA6hAkDJgp28BF0G0aSe9Ml64S9cIwch/edit?usp=sharing&ouid=104686423957654811582&rtpof=true&sd=true

Any help is appreciated

I wrote this query to get the daily answer.

select first_order_date,  sum(first_total_order) as tot from
      (select user_id,min(order_date) as first_order_date,total_order as 
        first_total_order
         from
          (select userId as user_id,date(createdAt) as order_date,count(orderId) 
            as total_order
              from userorder
              group by 1,2
             ) as uo
        group by 1) z
  group by 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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