我正在使用 Postgres 9.6。我有这样的查询:
SELECT name, count(spend) as spend FROM mytable GROUP BY name;
这会产生类似 Dave,40000
的结果。
我想为每个结果添加一个唯一标识符(是的,我知道名称已经是一个唯一标识符,但我想要一个数字)。所以我想要像 1,Dave,40000
这样的结果。
唯一标识符是什么并不特别重要,只要它们是数字即可,但它们也可能是整数。
有没有一种简单的方法可以使用 Postgres 做到这一点?
最佳答案
一个窗口函数:
select row_number() over(order by name) as ui, name, count(spend) as spend
from mytable
group by name;
https://www.postgresql.org/docs/current/static/functions-window.html
关于postgresql - Postgres : add unique integer to results?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42587172/