Oracle SQL 缺失年份计算最后的价值和复利
Oracle SQL Missing Years Calculate Last Value and Compound Interest
我在 Oracle 中有以下 table:
F1
F2
年
AMT
DC1
123
2021
1000
DC1
123
2022
1100
DC1
123
2023
DC1
123
2024
DC2
456
2021
5000
DC2
456
2022
6000
DC2
456
2023
DC2
456
2024
我想根据最后一个可用日期( YEAR
2022) 并乘以 AMT
每年复利 2.1%。
我用过LAST_VALUE
函数
`(LAST VALUE(AMT IGNORE NULLS) OVER (PARTITION BY F1, F2 ORDER BY F1 F2 YEAR ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS NEXT_AMT`
获取每个分组的最后一个值(1100 和 6000),但是当我乘以 2.1% 时,我没有得到复利效应。
期望的输出:
F1
F2
年
AMT
NEXT_AMT
NEW_AMT
DC1
123
2021
1000
1000
DC1
123
2022
1100
1100
DC1
123
2023
1100
1123.1
DC1
123
2024
1100
1146.68
DC2
456
2021
5000
5000
DC2
456
2022
6000
6000
DC2
456
2023
6000
6126
DC2
456
2024
6000
6254.65
计算比率然后用算术做乘法:
with t as (
select t.*,
max(case when year = 2022 then amt end) over (partition by f1, f2) as year_2022,
(max(case when year = 2022 then amt end) over (partition by f1, f2) /
max(case when year = 2021 then amt end) over (partition by f1, f2)
) as ratio
from mytable t
)
select t.*,
coalesce(amt,
year_2022 * power(ratio, year - 2022)
) as new_amt
from t;
编辑:
糟糕,我看错了问题。您实际上有一个固定的比例来增加金额。那更容易:
with t as (
select t.*,
max(case when year = 2022 then amt end) over (partition by f1, f2) as year_2022
from mytable t
)
select t.*,
coalesce(amt,
year_2022 * power(1.021, year - 2022)
) as new_amt
from t;
我也意识到您可能不想硬编码 2022。所以:
with t as (
select t.*,
last_value(case when amt is not null then year end ignore nulls) over (partition by f1, f2 order by year) as last_year,
last_value(amt ignore nulls) over (partition by f1, f2 order by amt) as last_year_amt
from mytable t
)
select t.*,
coalesce(amt,
last_year_amt * power(1.021, year - last_year)
) as new_amt
from t;
Here 是一个 db<>fiddle.
我在 Oracle 中有以下 table:
F1 | F2 | 年 | AMT |
---|---|---|---|
DC1 | 123 | 2021 | 1000 |
DC1 | 123 | 2022 | 1100 |
DC1 | 123 | 2023 | |
DC1 | 123 | 2024 | |
DC2 | 456 | 2021 | 5000 |
DC2 | 456 | 2022 | 6000 |
DC2 | 456 | 2023 | |
DC2 | 456 | 2024 |
我想根据最后一个可用日期( YEAR
2022) 并乘以 AMT
每年复利 2.1%。
我用过LAST_VALUE
函数
`(LAST VALUE(AMT IGNORE NULLS) OVER (PARTITION BY F1, F2 ORDER BY F1 F2 YEAR ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS NEXT_AMT`
获取每个分组的最后一个值(1100 和 6000),但是当我乘以 2.1% 时,我没有得到复利效应。
期望的输出:
F1 | F2 | 年 | AMT | NEXT_AMT | NEW_AMT |
---|---|---|---|---|---|
DC1 | 123 | 2021 | 1000 | 1000 | |
DC1 | 123 | 2022 | 1100 | 1100 | |
DC1 | 123 | 2023 | 1100 | 1123.1 | |
DC1 | 123 | 2024 | 1100 | 1146.68 | |
DC2 | 456 | 2021 | 5000 | 5000 | |
DC2 | 456 | 2022 | 6000 | 6000 | |
DC2 | 456 | 2023 | 6000 | 6126 | |
DC2 | 456 | 2024 | 6000 | 6254.65 |
计算比率然后用算术做乘法:
with t as (
select t.*,
max(case when year = 2022 then amt end) over (partition by f1, f2) as year_2022,
(max(case when year = 2022 then amt end) over (partition by f1, f2) /
max(case when year = 2021 then amt end) over (partition by f1, f2)
) as ratio
from mytable t
)
select t.*,
coalesce(amt,
year_2022 * power(ratio, year - 2022)
) as new_amt
from t;
编辑:
糟糕,我看错了问题。您实际上有一个固定的比例来增加金额。那更容易:
with t as (
select t.*,
max(case when year = 2022 then amt end) over (partition by f1, f2) as year_2022
from mytable t
)
select t.*,
coalesce(amt,
year_2022 * power(1.021, year - 2022)
) as new_amt
from t;
我也意识到您可能不想硬编码 2022。所以:
with t as (
select t.*,
last_value(case when amt is not null then year end ignore nulls) over (partition by f1, f2 order by year) as last_year,
last_value(amt ignore nulls) over (partition by f1, f2 order by amt) as last_year_amt
from mytable t
)
select t.*,
coalesce(amt,
last_year_amt * power(1.021, year - last_year)
) as new_amt
from t;
Here 是一个 db<>fiddle.