【发布时间】: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
【问题讨论】: