vol*_*ron 70 sql database postgresql
我想显示PostgreSQL查询返回的每条记录的观察数.
我认为在8.4窗口函数中可以执行此功能.
vol*_*ron 103
select row_number() over (order by <field> nulls last) as rownum, *
from foo_tbl
order by <field>
Run Code Online (Sandbox Code Playgroud)
如果没有必要订购,这个答案也可以简化:
select row_number() over(), * -- notice: no fields are needed
from foo_tbl
Run Code Online (Sandbox Code Playgroud)
对于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)
Run Code Online (Sandbox Code Playgroud)
希望这有助于某人.