I have a table transactions and I trying to figure out our new customers in a given month. That means that if a customer didn't have a transaction in the time before the month he/she counts as a new customer.
I have figured out a way, but it is seriously inefficient and takes ages. I then came across this artikel which compares different methods. I have tried to adjust that approach to mine without success.
To visualise my problem:
|--------------------------- time period with all transactions -----------------------|
|----- period before month transactions = 0) ---|---- curr month transactions > 0 ----|
The table looks like this:
transactions
id, email, state, date_paid
My query:
SELECT
l.email
FROM
transactions as l
LEFT JOIN transactions as r ON r.email = l.email
WHERE
r.email IS NULL
AND l.state = 'paid'
AND r.state = 'paid'
AND l.date_paid <= '2013-12-31 23:59:59'
AND r.date_paid < '2013-12-01 00:00:00'
What am I doing wrong?
Try this:
SELECT l.email
FROM transactions AS l
LEFT JOIN transactions AS r ON r.email = l.email AND r.state = 'paid' AND r.date_paid < '2013-12-01 00:00:00'
WHERE r.email IS NULL AND l.state = 'paid' AND l.date_paid <= '2013-12-31 23:59:59'
try this:
SELECT l.email
FROM transactions l
WHERE NOT l.email IN (SELECT r.email
FROM transactions r
WHERE r.state = 'paid' AND r.date_paid < '2013-12-01 00:00:00')
AND l.state = 'paid' AND l.date_paid <= '2013-12-31 23:59:59'
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.