我有类似这样的数据
YearMonth Recovery Segment
201701 0.002 NULL
. . .
. . .
201712 0.002 NULL
201801 0.002 NULL
. . .
. . .
201812 0.002 NULL
同样,我有10年的数据,我想使用“恢复”列(在整个10年中截然不同)填充“细分列”。对于第一年201701-201712,细分为0.002,那么对于下一年201801-201812,其细分为0.002 *(1- 0.2)= 0.0004,而对于201901-201912则为0.0004 *(1-0.2),依此类推。 >
我曾尝试过使用join逻辑,但一年后无法前进。任何帮助将非常感激。预先谢谢你。
新数据看起来像这样
YearMonth Recovery Segment
201701 0.002 0.002
. . .
. . .
201712 0.002 0.002
201801 0.002 0.0004
. . .
. . .
201812 0.002 0.0004
答案 0 :(得分:0)
DECLARE @Source TABLE
(
YearMonth INT,
Recovery DECIMAL(20, 10),
Segment DECIMAL(20, 10)
);
WITH months
AS (SELECT TOP (10 * 12)
ROW_NUMBER() OVER (ORDER BY t1.object_id) AS MonthNo
FROM sys.all_columns t1
INNER JOIN sys.all_columns t2
ON t1.object_id = t2.object_id),
dates (d)
AS (SELECT DATEADD(MONTH, MonthNo - 1, '20170101')
FROM months)
INSERT INTO @Source
(
YearMonth,
[Recovery]
)
SELECT YEAR(d) * 100 + MONTH(d),
0.002
FROM dates;
UPDATE @Source
SET Segment = [Recovery] * POWER(1 - 0.20000, YearMonth / 100 - 2017);
SELECT *
FROM @Source;
答案 1 :(得分:0)
我会将此作为算术问题来解决:
select t.*,
recovery * power(1 - 0.2, convert(int, left(yearmonth, 4)) - 2017) as segment
from t;
0.2
减少了20%。
如果您确实要设置该值:
update t
set segment = recovery * power(1 - 0.2, convert(int, left(yearmonth, 4)) - 2017);