Solved

Issue with getting unwated duplicate records in SQL query

Posted on 2013-12-01
3
471 Views
Last Modified: 2013-12-02
I have a database of baseball stats, one record per player per year, and I want to generate a list of players that meet certain criteria two years in a row.  But I'm running into issues with players getting doubled up when they meet the criteria for more than two years.

The core structure looks something like this:

select PLAYER
from
TABLE a,
TABLE b
where a.YEAR = b.YEAR+1

But when a player meets the criteria for two pairs of years, like this...

Player A | 2011 | 2012
Player A | 2012 | 2013

... you wind up doubling him up.  In this instance, his 2012 season would get counted twice.  I'd want the Player to be on my list as such:

Player A | 2011
Player A | 2012
Player A | 2013

...but right now I'm getting:

Player A | 2011
Player A | 2012
Player A | 2012
Player A | 2013

Can anyone provide a solution for only including that middle year once?
0
Comment
Question by:dresdena1
  • 2
3 Comments
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 39688934
you should use either use the GROUP BY or DISTINCT keyword

ie

SELECT DISTINCT PLAYER
TABLE a,
TABLE b
where a.YEAR = b.YEAR+1
0
 

Author Closing Comment

by:dresdena1
ID: 39689293
Not exactly what I was looking for, but led me in the right direction.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39689539
No problem. Can you post how you eventually did it?
As for the grading, this should probably be a B.
I am thinking you may not be clear on EE grading guidelines for Questions.

http://www.experts-exchange.com/help.jsp?hi=400#hs=29&hi=403

Grading Guidelines

Grading at Experts Exchange is not like school. It's more like the  "10-point Must" system in professional boxing; in other words, an answer  is worth an A, unless it doesn't resolve your issue. If it requires you  to do a little more research, or figure out one more piece of code,  then it's worth a B. If you think it's not worth a B, the custom is to  offer the Experts an opportunity to earn a better grade.

If this answer was the answer to your question, the proper grade to give, even if the answer is not liked, is an A.  To change the grade, you can look at the directions here:

http://www.experts-exchange.com/help.jsp?hi=400#hs=29&hi=404

It says:

If you feel you have graded a solution incorrectly, click the Request  Attention button and the Moderators will change it for you. If another  member disputes the grade you have given, you will be asked to explain  your reasoning; please respond to those requests.

I always think it is better to explain to the Author the grading system before I take a Question to the Moderators.  More often than not, Authors don't really understand how EE grades are supposed to work.

Rob
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

920 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now