如何获得不同组合的最长日期

时间:2019-06-07 18:15:04

标签: sql-server date max aggregate-functions

我正在尝试查找与最大数据相关的数据,而不是所有数据的最大日期。我搜索了类似的问题,但似乎我的问题有所不同。我检查了

How can I SELECT rows with MAX(Column value), DISTINCT by MULTIPLE columns in SQL Get Max Date - For Every Transaction

这是我的代码,它返回所有数据:

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

但是我需要返回与(家庭,运动,位置,名字)组合的最大日期相关的数据,如下所示:

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 

这些是家庭表和名字表之间的关系 familyRelation表

familyId  firstNameID
100          10
100          20
200          30
300          40

家庭表

NameID     name
10         mary
20         susan 
30         henry
40         grant
100        smith
200        jones
300        adams

我尝试了此方法,但是它对所有行使用了相同的最大日期,这不是我想要的,因为(家庭,运动,位置,名字)的不同组合具有不同的最大日期:

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

感谢您的所有帮助。我似乎无法弄清楚。

更新:我尝试使用row_number(),但是代码仍未返回我需要的结果。我需要添加a.awardAmount,但是一旦添加,就没有上限日期了,因为奖励金额不同

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 

1 个答案:

答案 0 :(得分:1)

听起来像您在寻找一个简单的分组对象-

SELECT
      top.name as [familyName], 
      s.sportName,
      p.position, 
      bottom.firstName as [firstName], 
      a.awardAmount, 
      MAX(a.awardDate) as maxDatePerGroup
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 
GROUP BY top.name, s.sportName, p.position,bottom.firstName, a.awardAmount