我需要你的帮助 我需要像这样计算我的专栏中的数据。
如何计算列初始余额中的数据?
在初始余额栏中,数据按net_01计算,直至net_04逐年连续计算。
目前,我正致力于oracle 12c。是否有任何功能来实现这一目标?
任何帮助将不胜感激,谢谢。
答案 0 :(得分:0)
对MERGE
分析函数使用SUM
语句:
Oracle 11g R2架构设置:
CREATE TABLE table_name ( year, initial_balance, net_01, net_02, net_03, net_04 ) AS
SELECT 2013, 0, 100, 100, 100, 100 FROM DUAL UNION ALL
SELECT 2014, 0, -200, 100, 100, 100 FROM DUAL UNION ALL
SELECT 2015, 0, -300, 100, 100, 100 FROM DUAL UNION ALL
SELECT 2016, 0, 0, 0, 0, 0 FROM DUAL UNION ALL
SELECT 2017, 0, 0, 0, 0, 0 FROM DUAL;
查询1 :
MERGE INTO table_name dst
USING (
SELECT ROWID rid,
SUM( net_01 + net_02 + net_03 + net_04 )
OVER (
ORDER BY year
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
) AS initial_balance
FROM table_name
) src
ON ( dst.ROWID = src.RID )
WHEN MATCHED THEN
UPDATE SET
initial_balance = COALESCE( src.initial_balance, 0 )
<强> Results 强>: 查询2 :
SELECT * FROM table_name
<强> Results 强>:
| YEAR | INITIAL_BALANCE | NET_01 | NET_02 | NET_03 | NET_04 |
|------|-----------------|--------|--------|--------|--------|
| 2013 | 0 | 100 | 100 | 100 | 100 |
| 2014 | 400 | -200 | 100 | 100 | 100 |
| 2015 | 500 | -300 | 100 | 100 | 100 |
| 2016 | 500 | 0 | 0 | 0 | 0 |
| 2017 | 500 | 0 | 0 | 0 | 0 |