我想显示PostgreSQL查询返回的每条记录的观察编号。
我认为在8.4窗口函数中可以执行此功能。
答案 0 :(得分:99)
select row_number() over (order by <field> nulls last) as rownum, *
from foo_tbl
order by <field>
如果没有必要订购,也可以简化这个答案:
select row_number() over(), * -- notice: no fields are needed
from foo_tbl
答案 1 :(得分:5)
对于8.4之前的版本:
SELECT count(*) rownum, foo.*
FROM datatable foo
JOIN datatable bar
ON (foo.pk_id < bar.pk_id)
GROUP BY foo.pk_id, foo.a, foo.b
ORDER BY rownum
;
-- if there isn't a single unique/primary key field, you can concatenate fields
-- Example: ON (foo.a||foo.b||foo.c < bar.a||bar.b||bar.c)
希望这有助于某人。
答案 2 :(得分:-1)
仅查询不带*
的设备,将输出返回的总行数。
select from table_name ...;