简体   繁体   中英

get total of multiple columns using Oracle SQL

I'm having a problem on how to get the total of columns Amount1 - Amount5 each line.

I tried the below query but the result is always 0.

Select ID, Amount1 + Amount2 + Amount3 + Amount4 as total_amount from table

ID Amount1 Amount2 Amount3 Amount4
1 5 5 5 5
2 10 10 10 10
3 15 20 20 20

Hope you can help me on this.

Thanks,

Result can't be 0 for data you posted:

SQL> with test (id, amount1, amount2, amount3, amount4) as
  2    (select 1,  5,  5,  5,  5 from dual union all
  3     select 2, 10, 10, 10, 10 from dual union all
  4     select 3, 15, 20, 20, 20 from dual
  5    )
  6  select id,
  7         amount1 + amount2 + amount3 + amount4 as total_amount
  8  from test
  9  order by id;

        ID TOTAL_AMOUNT
---------- ------------
         1           20
         2           40
         3           75

SQL>

Therefore, either you did something wrong, or didn't post everything we should know to assist.

Maybe you use two connections to the same user; for one, you updated old values for all amount columns to what you posted, but the one that actually runs the select statement, they are still 0 because - in the first session - you didn't COMMIT .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM