我有一个表交易,我试图找出给定月份的新客户。这意味着如果客户在月份之前没有交易,他/她将被视为新客户。
我找到了一种方法,但它效率很低,需要很长时间。然后我遇到了这个比较不同方法的artikel。我试图将这种方法调整到我的方法而没有成功。
想象我的问题:
|--------------------------- time period with all transactions -----------------------|
|----- period before month transactions = 0) ---|---- curr month transactions > 0 ----|
表格如下:
transactions
id, email, state, date_paid
我的查询:
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'
我做错了什么?
答案 0 :(得分:0)
试试这个:
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'
答案 1 :(得分:0)
试试这个:
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'