Link to home
Start Free TrialLog in
Avatar of BHUC
BHUCFlag for United States of America

asked on

Find customers who ordered in 2015 but not 2016

I have  a table called Passes1 that shows every ticket sale we have ever made.
I want to run a query on customers that bought a ticket in 2015 but have not bought yet in 2016

I have this query -
SELECT * FROM Passes1 as pa WHERE NOT EXISTS (Select * from Passes1 as pa1 WHERE pa.THID=pa1.THID AND pa1.Year = '2016') AND Year ='2015'

Where am I off?
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

or you do a LEFT JOIN:

select *
from Passes1 as P2015
left join Passes as P2016
on P2016.Year = '2016'
and P2016.THID = P2015.THID
where P2015.Year = '2015'
and P2016.THID is null

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Olaf Doschke
Olaf Doschke
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
...more likely than a Year column you would have an orderdate column and would pick MAX(YEAR(orderdate)) = 2015

Bye, Olaf.