MySQL: how to sum up vaues of rows and sort the result?

I'm looking for a SQL-statement that sums up some specific values and sorts the result. To be more clear: I have a table that consists of identifiers and values:

id     val
ab     10
ab     12
ab      3
cd     25
cd     10
ef      2
ef      7

Here all values for ab, cd and ef have to be summed up and ordered by result so that I get the following:

cd     35
ab     25
ef      9

So is there a SQL-statement that performs that task in one go?

Answers


SELECT id, SUM(val) as total
FROM your_table
GROUP BY id
ORDER BY total DESC;

Use group function sum:

select id, sum(val) as val from my_table group by id order by 2 desc

Need Your Help

Linking Ruby with HTML Code

html css ruby opalrb

I am aware that you can implement Javascript code to an HTML documents, by inserting the code between the <script></script> tags.. is there a similar way to do this with Ruby?