2009. 10. 8.

[Oracle] 함수 계산 누적 값 더하기

Quiz> 아래와 같이 결과 값을 만들어라.
COL1 COL2
---- ----------
A 10
C 20
D 10
B 30

COL1 COL2 SUM
---- ---------- ----------
A 10 10
C 20 30
D 10 40
B 30 70


[풀이]
SQL> create table exam1(
2 id varchar2(2),
3 num number)
4 ;
SQL> insert into exam1 values('A',10);

1 row created.

SQL> insert into exam1 values('C',20);

1 row created.

SQL> insert into exam1 values('D',10);

1 row created.

SQL> insert into exam1 values('B',30);

1 row created.

SQL> select * from exam1;

ID NUM
---- ----------
A 10
C 20
D 10
B 30

SQL>
1 select min(y.id) col1, min(y.num) col2, sum(x.num) sss
2 from exam1 x, exam1 y
3 where x.rowid<=y.rowid
4 group by y.rowid
5* order by sss

COL1 COL2 SSS
---- ---------- ----------
A 10 10
C 20 30
D 10 40
B 30 70

댓글 없음: