开发者

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

开发者 https://www.devze.com 2023-04-12 01:45 出处:网络
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:

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

精彩评论

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

关注公众号