How do I write a query that outputs the row number

2019-04-19 18:20发布

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

and I want to write a statement, without writing a procedure or view if possible, that gives me

1 John
2 Paul
3 George
4 Ringo

标签: sql db2
3条回答
该账号已被封号
2楼-- · 2019-04-19 19:01

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.

查看更多
家丑人穷心不美
3楼-- · 2019-04-19 19:04
SELECT ROW_NUMBER() OVER (ORDER BY beatle_name ASC) AS ROWID, * FROM beatles
查看更多
Evening l夕情丶
4楼-- · 2019-04-19 19:15
SELECT ROW_NUMBER() OVER(ORDER BY BEATLE_NAME) ROWNUM,BEATLE_NAME FROM BEATLES;
查看更多
登录 后发表回答