WITH A AS ( SELECT '철수' NM, 1 I, 2 O FROM DUAL
UNION ALL
SELECT '철수' NM, 2 I, 1 O FROM DUAL
UNION ALL
SELECT '철수' NM, 3 I, 3 O FROM DUAL
UNION ALL
SELECT '영희' NM, 3 I, 3 O FROM DUAL
UNION ALL
SELECT '영희' NM, 4 I, 5 O FROM DUAL
UNION ALL
SELECT '영희' NM, 5 I, 2 O FROM DUAL)
SELECT NM,
SUM(I-O) OVER(PARTITION BY NM ORDER BY ROWNUM) - SUM(I-O) 원래잔액,
I 수입,
O 지출,
SUM(I-O) OVER(PARTITION BY NM ORDER BY ROWNUM) 이후잔액
FROM A
GROUP BY NM,I,O,ROWNUM
ORDER BY ROWNUM