• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • Last Modified:

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
0
jazjef
Asked:
jazjef
  • 8
  • 7
  • 2
  • +2
1 Solution
 
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 8
  • 7
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now