Get Latest Rates For Each Distinct Rate Name

删除回忆录丶 提交于 2020-01-17 15:08:30

问题


RateName     Rate    EffectiveDate
--------     ----    -------------
RateOne      400     2011-01-05
RateOne      410     2011-06-31
RateTwo      147     2010-09-21
RateThree    68      2011-07-14
RateTwo      100     2011-10-30

If I have data such as the above, how can I run a query such that I'll have these as results:

RateName     Rate    EffectiveDate
--------     ----    -------------
RateOne      410     2011-06-31
RateThree    68      2011-07-14
RateTwo      100     2011-10-30       

Basically, I just need the latest rates for each distinct rate name.


回答1:


You can try this:

SELECT A.*
FROM YourTable A
INNER JOIN (    SELECT RateName, MAX(EffectiveDate) AS MaxDate
                FROM YourTable
                GROUP BY RateName) B
ON A.RateName = B.RateName AND A.EffectiveDate = B.MaxDate


来源:https://stackoverflow.com/questions/6700619/get-latest-rates-for-each-distinct-rate-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!