我创建了以下查询,并尝试在最后一行中创建另一个初始值为1000的列,然后将原始1000乘以mdp.DataPointValue,然后在初始1000之后使用新计算的值继续
例如,在5/25/10我希望起始值为1000.在5/26/10我希望它是mdp.DataPointValue *上一个值(第一个为1000)依此类推。任何帮助将不胜感激
SELECT AsOfDate,
mdp.DataPointValue
FROM DataPointPITs AS mdp
WHERE mdp.DataPointId = '2710' AND AsOfDate > '2010-05-25'
ORDER BY AsOfDate DESC
答案 0 :(得分:1)
这听起来像是LAG()
SELECT AsOfDate,
mdp.DataPointValue,
mdp.DataPointValue * LAG(mdf.DataPointValue, 1, 1000) OVER (ORDER BY AsOfDate DESC)
FROM DataPointPITs AS mdp
WHERE mdp.DataPointId = '2710' AND AsOfDate > '2010-05-25'
ORDER BY AsOfDate DESC
欢迎来到窗口功能奇妙的古怪世界。
答案 1 :(得分:0)
假设值始终为正(从不为负或零)并且没有间隙,您可以使用SUM()
和算术执行累积产品:
SELECT AsOfDate, mdp.DataPointValue,
1000 * exp(sum(log(mdp.DataPointValue)) over (order by AsOfDate))
FROM DataPointPITs mdp
WHERE mdp.DataPointId = '2710' AND AsOfDate > '2010-05-25'
ORDER BY AsOfDate DESC;