SQL 如何在Postgres中分组并返回总和行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8004655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:08:30 来源:igfitidea点击:
how to group by and return sum row in Postgres
提问by kuslahne
I am stuck here. I have row in Postgres like this:
我被困在这里。我在 Postgres 中有这样的行:
id amount
a 5000
a 1500
a 500
b 2000
b 1000
c 4000
How is the sql syntax to get result like this?
获得这样的结果的 sql 语法如何?
id amount
a 7000
b 3000
c 4000
回答by Marc B
SELECT id, SUM(amount) AS amount
FROM yourtable
GROUP BY id