PostgreSQL unnest() with element number
Postgres 9.4 or later Use WITH ORDINALITY for set-returning functions: When a function in the FROM clause is suffixed by WITH ORDINALITY, a bigint column is appended to the output which starts from 1 and increments by 1 for each row of the function’s output. This is most useful in the case of set returning … Read more