I have a database which is tracking the date a transaction takes place, in addition to the unique buyer that transaction corresponds to - now I am trying to look at reservations which resulted in a customer purchasing again at a later date. Right now, the code and sample output below shows me which customers were repeat customers by the count of their buyer_id, but I also want to be able to to see which purchases (reservations) resulted in that same customer purchasing again at a later time (not earlier in time, which could be the case by using a simple "Count").
SELECT r.id AS Reservation_id, r.created, r.buyer_id, COUNT(r.buyer_id)
FROM reservations r
GROUP BY r.buyer_id
ORDER BY Reservation_id
Reservation_id created buyer_id COUNT(r.buyer_id)
3 2007-08-14 18:28:38 438 1
7 2007-09-19 12:29:52 474 2
8 2007-09-19 13:14:54 476 1
9 2007-09-20 10:22:52 477 1
10 2007-09-25 15:27:45 485 3
11 2007-09-26 20:56:25 474 2
12 .... etc
The goal is to be able to pull additional data about each reservation and then see what factors of service have an effect on a customer coming back for a repeat purchase. In the case above, buyer #474 purchased twice, but I want to be able to distinguish the first purchase (when he/she did indeed come back a purchase again, the 2nd and final purchase) from the second purchase (after which no other purchases were made by buyer #474). In this case, the goal is to have another output row that shows:
Reservation_id created buyer_id COUNT(r.buyer_id) Returning
3 2007-08-14 18:28:38 438 1 0
7 2007-09-19 12:29:52 474 2 1
8 2007-09-19 13:14:54 476 1 0
9 2007-09-20 10:22:52 477 1 0
10 2007-09-25 15:27:45 485 3 1
11 2007-09-26 20:56:25 474 2 0
12 .... etc
i.e., showing how customer 474's ID does not show up again after reservations_id 11. I would do this in excel but I have a huge amount of rows and excel can't handle the functions over such a large dataset.
Any help or suggestions are appreciated.