根据前一行内的计算值创建计算值 [英] Create calculated value based on calculated value inside previous row

查看:43
本文介绍了根据前一行内的计算值创建计算值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法,将每月百分比变化应用于预测定价.我在 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...

Forecast Price = (Previous Row Forecast Price * Pct Change) + Previous Row Forecast Price

需要说明的是,我的数据中尚不存在黄色阴影价格.这就是我试图让我的查询计算.由于这是每月百分比变化,每一行都依赖于前一行并且超出了单个 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:

DECLARE @t TABLE(Date DATE, ID VARCHAR(10), Price DECIMAL(10, 2), PctChange DECIMAL(10, 2));
INSERT INTO @t VALUES
('2018-01-01', 'ABC', 100,    NULL),
('2018-01-02', 'ABC', 150,   50.00),
('2018-01-03', 'ABC', 130,  -13.33),
('2018-01-04', 'ABC', 120,  -07.69),
('2018-01-05', 'ABC', 110,  -08.33),
('2018-01-06', 'ABC', 120,    9.09),
('2018-01-07', 'ABC', 120,    0.00),
('2018-01-08', 'ABC', 100,  -16.67),
('2018-01-09', 'ABC', NULL, -07.21),
('2018-01-10', 'ABC', NULL,   1.31),
('2018-01-11', 'ABC', NULL,   6.38),
('2018-01-12', 'ABC', NULL, -30.00),
('2019-01-01', 'ABC', NULL,  14.29),
('2019-01-02', 'ABC', NULL,   5.27);

WITH ncte AS (
    -- number the rows sequentially without gaps
    SELECT *, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Date) AS rn
    FROM @t
), rcte AS (
    -- find first row in each group
    SELECT *, Price AS ForecastedPrice
    FROM ncte AS base
    WHERE rn = 1
    UNION ALL
    -- find next row for each group from prev rows
    SELECT curr.*, CAST(prev.ForecastedPrice * (1 + curr.PctChange / 100) AS DECIMAL(10, 2))
    FROM ncte AS curr
    INNER JOIN rcte AS prev ON curr.ID = prev.ID AND curr.rn = prev.rn + 1
)
SELECT *
FROM rcte
ORDER BY ID, rn

结果:

| Date       | ID  |  Price | PctChange | rn | ForecastedPrice |
|------------|-----|--------|-----------|----|-----------------|
| 2018-01-01 | ABC | 100.00 |      NULL |  1 |          100.00 |
| 2018-01-02 | ABC | 150.00 |     50.00 |  2 |          150.00 |
| 2018-01-03 | ABC | 130.00 |    -13.33 |  3 |          130.01 |
| 2018-01-04 | ABC | 120.00 |     -7.69 |  4 |          120.01 |
| 2018-01-05 | ABC | 110.00 |     -8.33 |  5 |          110.01 |
| 2018-01-06 | ABC | 120.00 |      9.09 |  6 |          120.01 |
| 2018-01-07 | ABC | 120.00 |      0.00 |  7 |          120.01 |
| 2018-01-08 | ABC | 100.00 |    -16.67 |  8 |          100.00 |
| 2018-01-09 | ABC |   NULL |     -7.21 |  9 |           92.79 |
| 2018-01-10 | ABC |   NULL |      1.31 | 10 |           94.01 |
| 2018-01-11 | ABC |   NULL |      6.38 | 11 |          100.01 |
| 2018-01-12 | ABC |   NULL |    -30.00 | 12 |           70.01 |
| 2019-01-01 | ABC |   NULL |     14.29 | 13 |           80.01 |
| 2019-01-02 | ABC |   NULL |      5.27 | 14 |           84.23 |

DB Fiddle 演示

这篇关于根据前一行内的计算值创建计算值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆