0

I have the following table:

column1 | column2 | column3
  1        3          4
  5        7          6

how do I sum the values of say, column 2 and 3, to return the sum?

The expected result is:

res
7
13
2
  • 3
    select (column2 + column3) as res from table Commented Jul 20, 2020 at 9:51
  • 1
    If those columns are NULLable you probably want to apply COALESCE: coalesce(column2,0) + coalesce(column3,0) Commented Jul 20, 2020 at 9:54

2 Answers 2

3

You can do maths within a select statement, so the following will work:

SELECT column2 + column3 AS res FROM table
Sign up to request clarification or add additional context in comments.

Comments

1

This works in postgresql.

  select sum(col2+col3) from (
  select col1, col2,col3,row_number() over() as rows from column_sum )  as foo 
  group by rows order by rows;

1 Comment

This is just a complicated way to express col2+col3 :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.