Joe
Joe

Reputation: 1067

MySQL Group By and Sum total value of other column

I have 2 columns like this:

word amount
dog 1
dog 5
elephant 2

I want to sum the amounts, to get the result

word amount
dog 6
elephant 2

What I have tried so far (and failed) is this:

SELECT word, SUM(amount) FROM `Data` GROUP BY 'word'

Upvotes: 57

Views: 113583

Answers (5)

Sani Kamal
Sani Kamal

Reputation: 1238

SELECT column_name1, SUM(column_name2) 
FROM table_name 
GROUP BY column_name1

Upvotes: 1

Chittaranjan Sethi
Chittaranjan Sethi

Reputation: 442

It should be grave accent symbol not single quote:

SELECT word, SUM( amount )
FROM Data
GROUP BY `word`;

Output:

word     SUM(amount)
dog           6
Elephant      2

enter image description here

Upvotes: 17

peter
peter

Reputation: 35

Try this unconventional approach.

SELECT word, SUM(amount) 
FROM Data 
Group By word

Upvotes: 1

Manish Sahu
Manish Sahu

Reputation: 325

SELECT word, SUM(amount) FROM Data Group By word;

Upvotes: 3

John Woo
John Woo

Reputation: 263893

Remove the single quote around the WORD. It causes the column name to be converted as string.

SELECT word, SUM(amount) 
FROM Data 
Group By word

Upvotes: 89

Related Questions