Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

How to select the row number in Postgres.

I tried this:

select

    row_number() over (ORDER BY cgcode_odc_mapping_id)as rownum,

    cgcode_odc_mapping_id

  from access_odc.access_odc_mapping_tb

  order by cgcode_odc_mapping_id

and got this error:

ERROR: syntax error at or near "over"

LINE 1: select row_number() over (ORDER BY cgcode_odc_mapping_id)as

I have checked these pages : How to show row numbers in PostgreSQL query?

This is my query:

 select row_number() over (ORDER BY cgcode_odc_mapping_id)as rownum,cgcode_odc_mapping_id from access_odc.access_odc_mapping_tb order by cgcode_odc_mapping_id 

this is the error:

ERROR: syntax error at or near "over" LINE 1: select row_number() over (ORDER BY cgcode_odc_mapping_id)as

1 Answer

0 votes
by (40.7k points)

Try this code:

SELECT tab.*,

    row_number() OVER () as rnum

  FROM tab;

For detailed information, you can refer to:

https://www.postgresql.org/docs/current/functions-window.html

Related questions

+3 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Dec 16, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer

Browse Categories

...