I'm looking to find the first 2 purchases for every customer.
I have a table that looks something like this:
orders
With columns:
order_id
customer_email
grand_total
purchase_date
There are many millions of rows in this table and I'm trying to look for an efficient way to grab the first two earliest records for each customer, so that I get a result set that looks something like this:
order_id
customer_email
purchase_date1
purchase_date2
I'm at a loss on how to do this efficiently.
order_idcan't be in your result set... either it'scustomer_email, order_id1, order_id2orcustomer_email, purchase_date1, purchase_date2. – Colin 't Hart Jun 28 '18 at 09:40order_idwill be different for the first two orders, so you (logically) should show the order_id for each order. Now,purchase_datecould actually be the same for the top two orders, but it could be different, so you should show both purchase dates as well. ... – RDFozz Jun 28 '18 at 16:51customer_emailuniquely identifies a specific customer; perhaps a customer may specify a different email for two orders, but still be recognized as the same customer. TL;DR:customer_email,order_id1,purchase_date,order_id2,purchase_date2might well be valid, as well - you aren't limited to just one field from the unique elements oforder– RDFozz Jun 28 '18 at 16:54