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?
BHUCAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
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

Olaf DoschkeSoftware DeveloperCommented:
Your code should run, but would list customers of 2015 with each ticket. If you only want customers then only pick out the DISTINCT custoemrid instead of * or group by customerid

Eg you could also query

Select customerid from passes1 where Max(Year)=2015 group by customerid

Open in new window


Bye, Olaf.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Olaf DoschkeSoftware DeveloperCommented:
...more likely than a Year column you would have an orderdate column and would pick MAX(YEAR(orderdate)) = 2015

Bye, Olaf.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.