How do I return a set of unique records based upon the greatest value in a certain field?

I'm using SQL Server Express 2008 and Management Studio...

I have multiple records that are returned on each player; I want to return only the records with the highest value for each player in the field called 'PO'.

*Attached JPEG has a snapshot of sample table data showing what I am currently getting and what I would like to get....

Many thanks...
sql-jpeg.JPG
LVL 4
jazjefAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

AnuroopsunddCommented:
SELECT lastname,firstname,yearID,teamID,POS, Max( PO ),A,E
FROM tablename
GROUP BY lastname
ORDER BY PO ASC
0
Pratima PharandeCommented:
SELECT X.lastname,X.firstname,X.yearID,Y.teamID,Y.POS,X.PO ,Y.A,Y.E
From tablename Y
inner join (
SELECT lastname,firstname,yearID, Max( PO )
FROM tablename
GROUP BY lastname,firstname,yearID ) X on X.lastname = Y.lastname and X.firstname = Y.firstname and X.yearID= Y.yearID
0
plummetCommented:
Hi, you can try this:


select * from 
(SELECT 
	*,
	rank() over (partition by lastname order by PO desc) as rank
FROM your_table) sub
where rank = 1

Open in new window


this should do it, I think!

Regards
John
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Rey Obrero (Capricorn1)Commented:
try this too

select *
from tableName
where PO= (select max([PO]) from tableName As T
      where T.lastname = TableName.lastname and T.firstname =TableName.firstname and T.yearID= TableName.yearID
      and T.TeamID= TableName.TeamID)
0
jazjefAuthor Commented:
I don't think I've given you all enough information....Here is the actual SQL statement that returns the record seen in the JPEG:


SELECT Master.nameFirst, Master.nameLast, Fielding.yearID, Fielding.teamID, Fielding.POS, Fielding.PO, Fielding.A, Fielding.E
FROM Master INNER JOIN Fielding ON Master.playerID = Fielding.playerID
WHERE((Fielding.yearID)=1985) AND (nameLast = 'Jones') AND (Fielding.teamID)='kca';


Notice how the nameFirst and nameLast come from a different table than the Fielding table where I get the PO value that must be assessed.....
0
plummetCommented:
How about this?

select * from 
	(SELECT 
	*,
	rank() over (partition by nameLast, nameFirst order by PO desc) as rank
	from
		(SELECT 
		Master.nameFirst, 
		Master.nameLast, 
		Fielding.yearID, 
		Fielding.teamID, 
		Fielding.POS, 
		Fielding.PO, 
		Fielding.A, 
		Fielding.E
		FROM Master INNER JOIN Fielding ON Master.playerID = Fielding.playerID
		) sub1
	) sub2
where rank = 1

Open in new window

0
jazjefAuthor Commented:
plummet:  It works..... you nailed it.....sort of...
The 'rank' thing seems to leave out some cases. I need to return the highest value of PO for each player comparing all their records and PO values. This 'rank' function seems to leave out some records and only returns about the top 8 records from about 22 possible that should be listed.
0
jazjefAuthor Commented:
This code doesn't return the record of Jones that has his highest PO value:

select * from
      (SELECT
      *,
      rank() over (partition by nameLast, nameFirst order by PO desc) as rank
      from
            (SELECT
            Master.nameFirst,
            Master.nameLast,
            Fielding.yearID,
            Fielding.teamID,
            Fielding.POS,
            Fielding.PO,
            Fielding.A,
            Fielding.E
            FROM Master INNER JOIN Fielding ON Master.playerID = Fielding.playerID
            ) sub1
      ) sub2
where (yearID)=1985 AND (nameLast = 'Jones') AND (teamID)='kca' AND rank = 1


I need the statement to look at all PO values for each player, find the highest one, then return only that record for each player. The result should be one record for each player on the team----and that record will be the one with the highest PO value for each.
0
Rey Obrero (Capricorn1)Commented:
try this query


SELECT A.*
FROM
(SELECT master.nameFirst, master.nameLast, Fielding.yearID, Fielding.teamID, Fielding.POS, Fielding.PO, Fielding.A, Fielding.E
FROM master INNER JOIN Fielding ON master.PlayerID = Fielding.PlayerID)
As A
Inner Join
(SELECT Max(Fielding.PO) AS MaxOfPO, Fielding.PlayerID, Fielding.yearID, Fielding.teamID
FROM Fielding
GROUP BY Fielding.PlayerID, Fielding.yearID, Fielding.teamID) As B
On A.[PO]=B.MaxOfPO
0
jazjefAuthor Commented:
capricorn:

You statement omits the following from my earlier post of the SQL statement I have thus far....

where (yearID)=1985 AND (nameLast = 'Jones') AND (teamID)='kca'

Your statement works; but I get every single player from ~1850 to 2011... I can't specify a specific, year, player, and team from any point in your code.

See the JPEG attached to my question.
0
Rey Obrero (Capricorn1)Commented:
try this

SELECT A.*
FROM (SELECT master.nameFirst, master.nameLast, Fielding.yearID, Fielding.teamID, Fielding.POS, Fielding.PO, Fielding.A, Fielding.E FROM master INNER JOIN Fielding ON master.PlayerID=Fielding.PlayerID)  AS A INNER JOIN (SELECT Max(Fielding.PO) AS MaxOfPO, Fielding.PlayerID, Fielding.yearID, Fielding.teamID FROM Fielding GROUP BY Fielding.PlayerID, Fielding.yearID, Fielding.teamID)  AS B ON A.[PO] = B.MaxOfPO
WHERE A.yearID=1985 AND A.nameLast="Jones" AND A.teamID="KCA"
0
jazjefAuthor Commented:
ATTENTION:

--SAMPLE FILE -- SAMPLE FILE -- SAMPLE FILE -- SAMPLE FILE --

I am attaching an .xls file that has a sample of the current recordset I am able to get, and an explanation of what I actually want. XLS file contains SQL statement as well ....
Get-unique-records-by-PO.xls
0
Rey Obrero (Capricorn1)Commented:
try my last post
0
jazjefAuthor Commented:
capricorn1:

This code below that you posted returns duplicate player records....13,374 total records to be exact .... It should be about 30 unique records to be the roster of a single major league baseball team.

SELECT A.*
FROM (SELECT master.nameFirst, master.nameLast, Fielding.yearID, Fielding.teamID, Fielding.POS, Fielding.PO, Fielding.A, Fielding.E FROM master INNER JOIN Fielding ON master.PlayerID=Fielding.PlayerID)  AS A INNER JOIN (SELECT Max(Fielding.PO) AS MaxOfPO, Fielding.PlayerID, Fielding.yearID, Fielding.teamID FROM Fielding GROUP BY Fielding.PlayerID, Fielding.yearID, Fielding.teamID)  AS B ON A.[PO] = B.MaxOfPO
WHERE A.yearID=1985 AND A.nameLast="Jones" AND A.teamID="KCA"
0
Rey Obrero (Capricorn1)Commented:
the query should return the unique record. the duplicates could be caused by duplicate records in your table

upload a copy of the db with the two tables..
0
Rey Obrero (Capricorn1)Commented:
try this query, added  And A.PlayerID=B.PlayerID to the join

SELECT A.*
FROM (SELECT master.nameFirst, master.nameLast, Fielding.yearID, Fielding.teamID, Fielding.POS, Fielding.PO, Fielding.A, Fielding.E FROM master INNER JOIN Fielding ON master.PlayerID=Fielding.PlayerID)  AS A INNER JOIN (SELECT Max(Fielding.PO) AS MaxOfPO, Fielding.PlayerID, Fielding.yearID, Fielding.teamID FROM Fielding GROUP BY Fielding.PlayerID, Fielding.yearID, Fielding.teamID)  AS B ON A.[PO] = B.MaxOfPO And A.PlayerID=B.PlayerID
WHERE A.yearID=1985 AND A.nameLast="Jones" AND A.teamID="KCA"
0
jazjefAuthor Commented:
Error message:

Invalid column name 'PlayerID'.


Error occurs where statement says "A.PlayerID" in the following line: AS B ON A.[PO] = B.MaxOfPO And A.PlayerID=B.PlayerID
0
Rey Obrero (Capricorn1)Commented:
try this

SELECT A.*
FROM (SELECT master.nameFirst, master.nameLast, Fielding.yearID, Fielding.teamID, Fielding.POS, Fielding.PO, Fielding.A, Fielding.E, Fielding.PlayerID FROM master INNER JOIN Fielding ON master.PlayerID=Fielding.PlayerID)  AS A INNER JOIN (SELECT Max(Fielding.PO) AS MaxOfPO, Fielding.PlayerID, Fielding.yearID, Fielding.teamID FROM Fielding GROUP BY Fielding.PlayerID, Fielding.yearID, Fielding.teamID)  AS B ON A.[PO] = B.MaxOfPO And A.PlayerID=B.PlayerID
WHERE A.yearID=1985 AND A.nameLast="Jones" AND A.teamID="KCA"
0

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
jazjefAuthor Commented:
capricorn1:
That did it. You nailed it. There are duplicate records remaining etc but that's really not part of my question. You code does what my question specifies and that's good with me. Thanks for sticking with me on this.....

Here's the code that works [I replaced quote marks with apostrophes for nameLast and teamID within the WHERE clause of capricorn1's original post]

SELECT A.*
FROM (SELECT master.nameFirst, master.nameLast, Fielding.yearID, Fielding.teamID, Fielding.POS, Fielding.PO, Fielding.A, Fielding.E, Fielding.PlayerID FROM master INNER JOIN Fielding ON master.PlayerID=Fielding.PlayerID)  AS A INNER JOIN (SELECT Max(Fielding.PO) AS MaxOfPO, Fielding.PlayerID, Fielding.yearID, Fielding.teamID FROM Fielding GROUP BY Fielding.PlayerID, Fielding.yearID, Fielding.teamID)  AS B ON A.[PO] = B.MaxOfPO And A.PlayerID=B.PlayerID
WHERE A.yearID=1985 AND A.nameLast='Jones' AND A.teamID='KCA'
0
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
Microsoft SQL Server 2008

From novice to tech pro — start learning today.