这是我的问题:
SELECT track_no,
(
CASE
WHEN credit_type IS NOT NULL
THEN credit_type
ELSE debit_type
END) AS transaction_type,
(
CASE WHEN credit_type IS NOT NULL
then credit_units else
debit_units end
) as units,
track_dt,
(select prv_free_units from benf_ewr_track where track_dt='28-JUN-17 04.51.17.291000000 PM') as openingbalance
FROM benf_ewr_track where track_dt BETWEEN TO_DATE ('2017/06/28', 'yyyy/mm/dd')
结果:
我有3个案例要加/减值。
答案 0 :(得分:1)
使用SUM( ... ) OVER ( ORDER BY ... )
分析函数:
SELECT *
FROM (
SELECT track_no,
COALESCE( credit_type, debit_type ) AS transaction_type,
COALESCE( credit_units, -debit_units ) as units,
track_dt,
SUM( COALESCE( credit_units, 0 ) - COALESCE( debit_units, 0 ) )
OVER ( ORDER BY track_dt )
AS Balance
FROM benf_ewr_track
)
WHERE track_dt BETWEEN DATE '2017-06-28' AND SYSDATE