Solved

Compare two rows in same table

Posted on 2014-04-27
6
1,282 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 19

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 35

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
How our DevOps Teams Maximize Uptime

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

 

Author Closing Comment

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

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

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Creating and Managing Databases with phpMyAdmin in cPanel.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

729 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