开发者

How to add up total of several rows that have a matching column

I have a query that returns something like:

COMPANY | TOTAL
--------+------
   A    |  10
   B    |  15
   C    |  10
   A    |  5
   A    |  10
   B    |  5
   D    |  10
开发者_如何学编程

Using this, I want to return results like:

COMPANY | TOTAL
--------+------
   A    |  25
   B    |  20
   C    |  10
   D    |  10

This has to be pretty simple, I just can't wrap my head around it.


Use this:

SELECT company, SUM(total)
FROM your_table
GROUP BY company

You can sort by sum appending

ORDER BY SUM(total) DESC

or by company

ORDER BY company


SELECT  company, SUM(total)
FROM    mytable
GROUP BY
        company
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜