Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculate sum

Status
Not open for further replies.

ang90

MIS
Aug 1, 2005
9
US
Hi,

I am trying to select sum of two columns in a table for each row ..

The table has ID, Value1, Value2 columns.

I am trying to select ID, Value1, value2, sum(value1, value2) for every row.

Can someone help?

Thanks.
 
Table test1

ID Value
1 100
2 150
3 175
4 200


Table test2

ID Value
1 400
2 300
3 700
4 800

select t1.id, t1.value value1,t2.value value2,t1.value + t2.value valueadd from test1 t1, test2 t2
where t1.id=t2.id;

ID value1 value2 valueadd
1 100 400 500
2 150 300 450
3 175 700 875
4 200 800 1000


Regards

Roland
 
Ang,

First, a hearty welcome to Tek-Tips. We hope your participation here will be rewarding and long standing.

Roedelfroe's solution is exactly what you want. Notice that the addition operation occurs between two operands (numeric expressions) whose values are known for that row:
Code:
select t1.id, t1.value value1,t2.value value2,[b]t1.value + t2.value[/b] valueadd from test1 t1, test2 t2
where t1.id=t2.id;

We use the SUM(<expression>) to add together values from a column:

Section 1 -- Sample data:
Code:
SQL> select dept_id, salary from s_emp;

   DEPT_ID     SALARY
---------- ----------
        50       3025
        41       1450
        31       1400
        10       1450
        50       1550
        41       1200
        42       1250
        43       1100
        44       1300
        45       1307
        31       1400
        32       1490
        33       1515
        34       1525
        35       1450
        41       1400
        41        940
        42       1200
        42        795
        43        750
        43        850
        44        800
        34        795
        45        860
        45       1100

25 rows selected.

Section 2 -- Invocation of the "SUM" function:
Code:
SQL> select dept_id, sum(salary)
  2  from s_emp
  3  group by dept_id;

   DEPT_ID SUM(SALARY)
---------- -----------
        10        1450
        31        2800
        32        1490
        33        1515
        34        2320
        35        1450
        41        4990
        42        3245
        43        2700
        44        2100
        45        3267
        50        4575

12 rows selected.
Let us know if this clarifies things a bit.


[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[ Providing low-cost remote Database Admin services]
Click here to join Utah Oracle Users Group on Tek-Tips if you use Oracle in Utah USA.
 
Mufasa,

Thank you. I was a bitr absent-minded that I forgot the + operator. It works . Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top