0

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     |
-------------------------
4
  • 2
    How did you get the numbers in your output from the numbers in your sample input? Commented Jul 30, 2014 at 15:13
  • @MichaelMcGriff I didn't :) Commented Jul 30, 2014 at 15:16
  • Where does the source table come from? Perhaps there's an unnecessary intermediate step here? Commented Jul 30, 2014 at 15:17
  • @Stromgren You'll get better assistance if you provide the correct output required based on the sample input instead of random numbers. Commented Jul 30, 2014 at 15:18

4 Answers 4

2

Try using SUM()

SELECT Country,
 SUM(`Count 1` + `Count 2`) AS count
FROM table_name 
GROUP BY country
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

Try this

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

Comments

0

Try this.

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

Comments

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.