问题描述
我正在尝试找到一种方法,将每月百分比变化应用于预测定价.我在 excel 中设置了我的问题,使其更清楚一些.我使用的是 SQL Server 2017.
I'm trying to find a way to apply monthly percentage changes to forecast pricing. I set my problem up in excel to make it a bit more clear. I'm using SQL Server 2017.
我们会说 18 年 9 月 1 日之前的所有月份都是历史月份,而 2018 年 9 月 1 日及以后的月份都是预测月份.我需要使用...计算预测价格(样本数据上的黄色阴影)
We'll say all months before 9/1/18 are historical and 9/1/18 and beyond are forecasts. I need to calculate the forecast price (shaded in yellow on the sample data) using...
需要说明的是,我的数据中尚不存在黄色阴影价格.这就是我试图让我的查询计算.由于这是每月百分比变化,每一行都依赖于前一行并且超出了单个 ROW_NUMBER/PARTITION 解决方案,因为我们必须使用之前计算出的价格.显然,excel 中的简单顺序计算在这里有点困难.知道如何在 SQL 中创建预测价格列吗?
Just to be clear, the yellow shaded prices do not exist in my data yet. That is what I am trying to have my query calculate. Since this is monthly percentage change, each row depends on the row before and goes beyond a single ROW_NUMBER/PARTITION solution because we have to use the previous calculated price. Clearly what is an easy sequential calculation in excel is a bit more difficult here. Any idea how to create forecasted price column in SQL?
推荐答案
您需要使用递归 CTE.这是查看前一行计算值的一种更简单的方法:
You need to use a recursive CTE. That is one of the easier ways to look at the value of a calculated value from previous row:
结果:
DB Fiddle 演示
这篇关于根据前一行内的计算值创建计算值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!