首页 > 数据库 > 数据库开发技术 > SQL案例分析:美联储降息前后的复利差距
WITH RECURSIVE investment(principal, total, years) AS (
SELECT 1000000.0, 1000000.0*(1+0.055), 1 -- 第一年投资收益
UNION ALL
SELECT total, total*(1+0.055), years+1 -- 第N年投资收益
FROM investment
WHERE years < 5
)
SELECT * FROM investment;
principal |total |years|
--------------------+-----------------------+-----+
1000000.0| 1055000.000| 1|
1055000.000| 1113025.000000| 2|
1113025.000000| 1174241.375000000| 3|
1174241.375000000| 1238824.650625000000| 4|
1238824.650625000000|1306960.006409375000000| 5|
WITH RECURSIVE investment(principal_before, total_before, principal_after, total_after, years) AS (
SELECT 1000000.0, 1000000*(1+0.055), 1000000.0, 1000000*(1+0.05), 1 -- 第一年投资收益
UNION ALL
SELECT total_before, total_before*(1+0.055), total_after, total_after*(1+0.05), years+1 -- 第N年投资收益
FROM investment
WHERE years < 5
)
SELECT principal_before, total_before, principal_after, total_after, years,
total_before - total_after AS diff
FROM investment;
principal_before |total_before |principal_after |total_after |years|diff |
--------------------+-----------------------+----------------+------------------+-----+---------------------+
1000000.0| 1055000.000| 1000000.0| 1050000.00| 1| 5000.000|
1055000.000| 1113025.000000| 1050000.00| 1102500.0000| 2| 10525.000000|
1113025.000000| 1174241.375000000| 1102500.0000| 1157625.000000| 3| 16616.375000000|
1174241.375000000| 1238824.650625000000| 1157625.000000| 1215506.25000000| 4| 23318.400625000000|
1238824.650625000000|1306960.006409375000000|1215506.25000000|1276281.5625000000| 5|30678.443909375000000|
原文作者:SQL编程思想;如有侵权,请联系管理员删除。