Select Row number in postgres

SqlPostgresqlWindow Functions

Sql Problem Overview


How to select 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 : https://stackoverflow.com/questions/3397121/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

Sql Solutions


Solution 1 - Sql

SELECT tab.*,
    row_number() OVER () as rnum
  FROM tab;

Here's the relevant section in the docs.

P.S. This, in fact, fully matches the answer in the referenced question.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaverickView Question on Stackoverflow
Solution 1 - SqlvyegorovView Answer on Stackoverflow