I have a table that stores membership records. One record per member, per year. eg:
MEMBER YEAR
--------------
steve 2011
steve 2010
bob 2010
jane 2011
What I'm trying to generate is a list of lapsed members (those who haven't renewed) for a given year. Something like "get all members who have an entry for 2010 but not 2011".
I've been wrestling with this one for a while and the closest examples I can find involve comparing two tables.
SELECT a.member
FROM YOUR_TABLE a
LEFT JOIN YOUR_TABLE b ON b.member = a.member
AND b.year = 2011
WHERE a.year = 2010
AND b.member IS NULL
SELECT a.member
FROM YOUR_TABLE a
WHERE a.year = 2010
AND NOT EXISTS (SELECT NULL
FROM YOUR_TABLE b
WHERE b.member = a.member
AND b.year = 2011)
SELECT a.member
FROM YOUR_TABLE a
WHERE a.year = 2010
AND a.member NOT IN (SELECT b.member
FROM YOUR_TABLE b
WHERE b.year = 2011)
Performance depends on if the columns compared are NULLable (the values can be NULL):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With