简体   繁体   English

使用 Oracle SQL 获得多列的总数

[英]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.我在如何获取每行 Amount1 - Amount5 列的总数时遇到问题。

I tried the below query but the result is always 0.我尝试了以下查询,但结果始终为 0。

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

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

Hope you can help me on this.希望你能在这方面帮助我。

Thanks,谢谢,

Result can't be 0 for data you posted:您发布的数据的结果不能为0

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 .一方面,您将所有amount列的旧值更新为您发布的内容,但实际运行select语句的值仍然0 ,因为 - 在第一个 session 中 - 您没有COMMIT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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