sql - MySQL Working out number of orders by customer in the previous year -
wondering how done in mysql - need number of orders in previous year order, if made 3 orders (1.1.13, 1.7.13 , 1.1.15 first , last orders 0 there no bookings in previous year middle 1 1 there booking 6 months before).
something gets first three, not sure how total?
select order_no, email, order_date table.orders order_no email order_date prev_yr_orders 143254 example@example.com 25/07/2013 1 646743 example@example.com 24/09/2013 2 757743 example@example.com 16/02/2014 3 993253 example@example.com 23/02/2014 4 535325 example@example.com 31/03/2015 1
you can using correlated subquery:
select o.*, (select count(*) orders o2 o2.email = o.email , o2.order_date < o.order_date , o2.order_date >= o.order_date - interval 1 year ) numordersprevyear orders o;
Comments
Post a Comment