【问题标题】:Pulling most recent value from a range of values从一系列值中提取最新值
【发布时间】:2015-11-09 19:31:58
【问题描述】:

我试图仅从字段 CDT.Value 中获取最近添加的值。就目前而言, Value = CDT.Value 不起作用,因为它试图返回多个值。 MAX(CDT.Value) 返回为每个字段组合添加的最大数字。 COUNT(CDT.Value) 返回为该值输入的不同数字的总数。

我想获取最近输入的值。 RPS.EffectiveStartDate 表示最近输入的日期/时间。基本上:CDT.Value at MAX(RPS.EffectiveStartDate)。

回答了很多类似的问题,其中大多数在子查询中实现了 SELECT,但我在尝试实现它们时没有得到任何有用的东西。

我认为子查询 SELECT 需要在 INNER JOIN vw_CurveDetail 语句之后出现,但不完全清楚应该如何完成。

How to find the record in a table that contains the maximum value?

SQL select max(date) and corresponding value

how do I query sql for a latest record date for each user

这是我正在处理的查询中的一个 sn-p:它很长,所以我不想包括整个事情

SELECT      RPS.MarketIdentifier
,           RPS.Jurisdiction
,           RPS.CurveType
,           RPS.RECType
,           MAX(RPS.EffectiveStartDate) EffectiveStartDate
,           CDT.ApplicationStartDate
,           Value = MAX(CDT.Value)
,           Year = CDT.Year
FROM        #RPSCurve                   RPS
INNER JOIN  vw_CurveDetail CDT ON   CDT.CurveIdentifier = RPS.CurveIdentifier
                                        AND CDT.EffectiveStartDate = RPS.EffectiveStartDate
INNER JOIN  #PlanningYearStartMonth     PYS ON  PYS.Jurisdiction = RPS.Jurisdiction

【问题讨论】:

    标签: sql subquery


    【解决方案1】:

    您可以在查询末尾添加此内容

    order by RPS.EffectiveStartDate desc
    

    【讨论】:

      【解决方案2】:

      为了只返回与最近的 EffectiveStartDate 相关的一行数据,您可以添加带有 ORDER BY 的 TOP 1 语句,如下所示:

      SELECT TOP 1      
                  RPS.MarketIdentifier
      ,           RPS.Jurisdiction
      ,           RPS.CurveType
      ,           RPS.RECType
      ,           RPS.EffectiveStartDate
      ,           CDT.ApplicationStartDate
      ,           CDT.Value
      ,           CDT.Year
      FROM        #RPSCurve RPS
      INNER JOIN  vw_CurveDetail CDT ON CDT.CurveIdentifier = RPS.CurveIdentifier
                                     AND CDT.EffectiveStartDate = RPS.EffectiveStartDate
      INNER JOIN  #PlanningYearStartMonth PYS ON PYS.Jurisdiction = RPS.Jurisdiction
      ORDER BY RPS.EffectiveStartDate DESC
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-20
        • 1970-01-01
        • 1970-01-01
        • 2021-06-14
        • 2019-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多