如何在PostgreSQL查询中显示行号?
我想显示PostgreSQL查询返回的每个logging的观察编号。
我认为在8.4窗口函数中可以执行这个function。
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
对于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)
希望这有助于某人。