nav100
nav100

Reputation: 3153

get sum of column values from SQL query

I have the following query and I am trying to get sum of values from Column2.

SELECT c.column1, c.column2
FROM table1 c 
LEFT JOIN table2 d ON d.column_id = c.column_id
Group by c.column1

column1         column2   
------------------------
survey_name1     10       
survey_name2     20

I would like to have a 3 rd column with sum of column2(i.e 10+20=30).I am not sure this is possible. Please suggest if there is any alternative solution available.

column1         column2  column3 
-------------------------------
survey_name1     10       30
survey_name2     20       30

Upvotes: 0

Views: 212

Answers (1)

Crono
Crono

Reputation: 10488

Unless you have a very good reason for having the exact same sum of values in each row, I'd strongly recommend that you use a second query to get the sum of column2 values instead:

SELECT  
  SUM(c.column2)
FROM table1 c 
    LEFT JOIN table2 d ON d.column_id = c.column_id
Group by c.column1

If that doesn't work for you please give more details on why you want sum on each rows.

EDIT:

If you really need sum on each row, you could cross join your original query with the one above. But really I can't think of a good reason to do this.

Upvotes: 1

Related Questions