How do I write a query that outputs the row number as a column?

后端 未结 3 1214
走了就别回头了
走了就别回头了 2021-02-19 06:19

How do I write a query that outputs the row number as a column? This is DB2 SQL on an iSeries.

eg if I have

table Beatles:

John
Paul
George
Ringo         


        
相关标签:
3条回答
  • 2021-02-19 06:57
    SELECT ROW_NUMBER() OVER (ORDER BY beatle_name ASC) AS ROWID, * FROM beatles
    
    0 讨论(0)
  • 2021-02-19 07:16

    Check out the row_number() function; you should be able to do this in DB2 via:

    SELECT row_number(), first_name FROM beatles
    

    I'm almost certain this is not part of the SQL standard though, so it is not likely to be portable should that ever be an issue.

    0 讨论(0)
  • 2021-02-19 07:21
    SELECT ROW_NUMBER() OVER(ORDER BY BEATLE_NAME) ROWNUM,BEATLE_NAME FROM BEATLES;
    
    0 讨论(0)
提交回复
热议问题