BatshevaRich
BatshevaRich

Reputation: 568

SQL sum values in columns for each row

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

Upvotes: 0

Views: 162

Answers (2)

Syed Mushtaq
Syed Mushtaq

Reputation: 321

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;

Upvotes: 1

Jim Wright
Jim Wright

Reputation: 6058

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

SELECT column2 + column3 AS res FROM table

Upvotes: 3

Related Questions