I am trying to find data related to the maximum data for a distinct combination of data, not the max date for all the data. I searched for similar problems but it seems my problem is different. I checked
How can I SELECT rows with MAX(Column value), DISTINCT by MULTIPLE columns in SQL Get Max Date - For Every Transaction
Here is my code that returns all data:
select top.name as [familyName], s.sportName, p.position, bottom.firstName as [firstName], a.awardAmount, a.awardDate
from familyRelation r
inner join family bottom on r.firstNameID = bottom.NameID
inner join family top on r.familyID = top.NameID
inner join familyPosition p on p.NameID = bottom.NameID
inner join sport s on s.sportID = p.sportID
left join awards a on a.awardID = p.familyPositionID
familyName sport position firstName award awardDate
Smith tennis umpire mary null 1/1/2011
Smith tennis umpire mary 100 10/20/2011
Smith swim diver mary null 1/1/2019
Smith diving diver susan 300 1/1/2011
Jones rugby player henry 100 1/1/2016
Jones rugby player henry 150 1/1/2011
Jones rugby forward henry 190 1/1/2008
Jones rugby forward henry 100 1/1/2011
Adams hockey goalkeeper grant null 1/1/2011
Adams hockey goalkeeper grant null 12/12/2018
Adams hockey goalkeeper grant null 1/1/2011
Adams hockey goalkeeper grant 5750 1/1/2011
but I need to return the data related to the maximum date for the (family, sport, position, firstName) combination as shown below:
familyName sport position firstName award awardDate
Smith tennis umpire mary 100 10/20/2011
Smith swim diver mary 150 1/1/2019
Smith diving diver susan 300 1/1/2011
Jones rugby player henry 190 1/1/2016
Jones rugby forward henry 100 1/1/2011
Adams hockey goalkeeper grant null 12/12/2018
These are the relations between the family and firstname tables familyRelation table
familyId firstNameID
100 10
100 20
200 30
300 40
family table
NameID name
10 mary
20 susan
30 henry
40 grant
100 smith
200 jones
300 adams
I tried this but it uses the same max date for all rows which is not what I want because different combinations of (family, sport, position, firstName) have different max dates:
select top.name as [familyName], s.sportName, p.position, bottom.firstName as [firstName], a.awardAmount, a.awardDate
from familyRelation r
inner join family bottom on r.firstNameID = bottom.NameID
inner join family top on r.familyID = top.NameID
inner join familyPosition p on p.NameID = bottom.NameID
inner join sport s on s.sportID = p.sportID
left join ( select awardAmount, max(awardDate)
from awards
group by awardAmount) a
on a.aID = p.p.familyPositionID
Thank you for all your help. I can't seem to figure this out.
update: I tried to use row_number() but the code is still not returning the results I need. I need to include a.awardAmount but once I add it, there is no maximum date because award amounts are distinct
select top.name as [familyName], s.sportName, p.position, bottom.firstName as [firstName], a.awardAmount, a.awardDate
, row_number() over partition by top.name, s.sportName, p.position, bottom.firstName
order by top.name, s.sportName, p.position, bottom.firstName)
from familyRelation r
inner join family bottom on r.firstNameID = bottom.NameID
inner join family top on r.familyID = top.NameID
inner join familyPosition p on p.NameID = bottom.NameID
inner join sport s on s.sportID = p.sportID
left join awards a on a.awardID = p.familyPositionID