zhanzezhu
zhanzezhu

Reputation: 121

SQL:How sum each columns in oracle?

select t.col1 a, t.col1+...+t.coln-1 b, t.col1+...+t.coln-1+t.coln c
from table

Can direct use b+t.col3?

I can think of:

with t1 as (select col1+...+coln-1 b from table)
select t2.col1 a, t1.b, t1.b+t2.coln c
from table t2
inner join t1 on t1.id=t2.id

Is there another way to do this.

Upvotes: 0

Views: 930

Answers (2)

David
David

Reputation: 1

I think this should work:

SELECT SUM(t.COL1) a, SUM(t.COL1 + t.COL2) b, SUM(t.COL1 + t.COL2 + t.COL3) c
FROM TABLE1 t

As mathguy mentioned below, you do not want to sum the columns and add totals, it was not so clear for me but now it make sense :)

SELECT t.COL1 a, t.COL1 + t.COL2 b, t.COL1 + t.COL2 + t.COL3 c
FROM TABLE1 t

will give you correct values.

Upvotes: 0

Hassan Iqbal
Hassan Iqbal

Reputation: 49

I think you can also do it in this way .

select a, b, b+col3
from (
select t.col1 a, t.col1+t.col2 b, t.col3
from t)

Upvotes: 3

Related Questions