Reputation: 1183
It's either been a long day or maybe it's time to retire :-)
I have a table of transactions of either purchase( id = 2) or allocation( id= 1) on registration. An allocation transactions occurs first before the customers can then transact.
I am looking to count the conversion rate, which is the number of people who registered and went ahead to make a purchase.
Here is how my table looks like
| id | transaction_type_id | customer_id | created_at |
| 234 | 1 | 22 | 2015-11-26
| 235 | 2 | 22 | 2015-11-26
| 236 | 1 | 23 | 2015-11-27
| 237 | 1 | 24 | 2015-11-27
| 238 | 1 | 25 | 2015-11-27
| 239 | 1 | 26 | 2015-11-28
| 240 | 2 | 26 | 2015-11-28
| 241 | 1 | 27 | 2015-11-28
| 242 | 1 | 28 | 2015-11-28
Here is the query I have so far
SELECT COUNT(t.id) AS total, DATE(t.transaction_date) as trans_date, (SELECT COUNT(t1.id) FROM transactions t1 WHERE t1.transaction_type_id = 1 AND t1.member_id = t.member_id) AS converted FROM transactions t WHERE t.transaction_type_id = 21 AND DATE(t.transaction_date) >= DATE(CURRENT_TIMESTAMP())-7 GROUP BY trans_date ORDER BY trans_date ASC
Upvotes: 0
Views: 24
Reputation: 360572
Since you want users with both trans types, and there possibly can be multiple of each:
SELECT t1.customer_id, count(t2.transaction_type_id)
FROM yourtable t1
LEFT JOIN yourtable t2 ON
(t1.customer_id = t2.customer_id AND t2.transaction_type_id = 2)
WHERE t1.transaction_type_id = 1
Basically: select out all the transtype=1 records, then self join to get all the customers that also have the transtype=2 records. That'll give you all of your "type 1" customers, and however many "type 2" records also exist for them. From that you can easily calculate total customers and how many of them actually purchased something.
Upvotes: 1