Stromgren
Stromgren

Reputation: 1074

SQL: Selecting sum of two columns across multiple rows

I have a table structure like this:

-------------------------------------
|  Country  |  Count 1  |  Count 2  |
-------------------------------------
|  USA      |  10       |  10       |
-------------------------------------
|  USA      |  10       |   10      |
-------------------------------------
|  France   |  200      |  200      |
-------------------------------------
|  USA      |  10       |  10       |
-------------------------------------
|  France   |  100      |  100      |
-------------------------------------

I would like to select the total of Count 1 and Count 2 for each country. So my output would be like

-------------------------
|  Country  |  Count    | 
-------------------------
|  USA      |    60     | 
-------------------------
|  France   |   600     |
-------------------------

Upvotes: 0

Views: 3759

Answers (4)

Md khirul ashik
Md khirul ashik

Reputation: 153

Try this.

UPDATE power 
  SET cnt = SUM(IFNULL(is_yellow, 0) + 
  IFNULL(is_green, 0) +  IFNULL(is_blue, 0));

Upvotes: 0

Taryn
Taryn

Reputation: 247710

This is pretty straight-forward, you can use the aggregate function SUM() with a GROUP BY:

SELECT country, 
  sum(count1 + count2) as Total
FROM yourtable
GROUP BY country;

See Demo

Upvotes: 2

user2989408
user2989408

Reputation: 3137

Try this

SELECT Country, SUM(Count_1 + Count_2)
FROM Table 
GROUP BY Country

Upvotes: 1

idmean
idmean

Reputation: 14875

Try using SUM()

SELECT Country,
 SUM(`Count 1` + `Count 2`) AS count
FROM table_name 
GROUP BY country

Upvotes: 2

Related Questions