Solved

Compare two rows in same table

Posted on 2014-04-27
6
1,073 Views
Last Modified: 2014-04-27
I have a table called Passes. In it we store each time a member purchases a season pass.

What I would like to do is compare and see who bought a pass in 2013, but not yet in 2014.

So entries are like this:

ID    HolderID    Year          Ticket
1          75          2013         Family
2          79          2013         Family
3          80          2013         Family
4          75          2014         Family
5          80          2014         Family

So the query would return that Holder 80 bought in 2013 but hasn't yet in 2014
0
Comment
Question by:BHUC
6 Comments
 
LVL 18

Expert Comment

by:Raheman M. Abdul
ID: 40026167
From the table, Holder 80 bought in 2013 as well as in 2014. is that correct?
0
 

Author Comment

by:BHUC
ID: 40026178
Sorry. Family 79 would show as not having bought in 2014 yet.
0
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 40026196
SELECT HolderID, MAX (Year) FROM Passes
WHERE HolderID NOT IN (
SELECT HolderID FROM Passes WHERE Year = '2014'
)
GROUP BY HolderID

This will show the IDs that have not bought a pass in 2014 and the most recent year when they bought a pass.

You can get rid of the GROUP BY if you only have 2013 and 2014 as years.

HTH,
Dan
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Closing Comment

by:BHUC
ID: 40026227
Thank you!
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40026229
You're welcome.

Glad I could help!
0
 
LVL 10

Expert Comment

by:ukerandi
ID: 40026234
SELECT HolderID, MAX (Year),Ticket FROM Passes
WHERE Year Not in( '2014')
GROUP BY HolderID,Ticket
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question