postgresql - Postgres : add unique integer to results?

标签 postgresql

我正在使用 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/

相关文章:

postgresql - 获取修改列名的触发程序

postgresql - ON CONFLICT 子句,如何用 DO UPDATE 替换 DO NOTHING?

sql - 带有包含条件的字符串的 if 语句

django - psycopg2 奇怪的行为

sql - Postgres 触发器不更新指定的列

postgresql - Postgres : first and last days of any month

postgresql - 由于docker上的权限被拒绝而无法创建postgres用户

postgresql - 即使 Postgres 正在运行并且凭据正确,mix ecto.create 也无法连接到 Postgres

python - 为什么 select_for_update 在并发插入中起作用?

postgresql - 错误 : operator does not exist: character varying = numeric