如何向使用order by?
的查询添加连续行号我想以这种形式提出请求:
SELECT row_number() over(), data
FROM myTable
ORDER BY data
这将产生所需的结果,因为行按“数据”排序,但行号也按数据排序。我理解这是正常的,因为我的行号是在订单之前生成的,但是如何在订单之后生成此行号?
我确实尝试使用这样的子查询:
SELECT row_number() over(ORDER BY data), *
FROM
(
SELECT data
FROM myTable
ORDER BY data
) As t1
如图here所示,但DB2似乎不支持此语法SELECT ..., * FROM
谢谢!
答案 0 :(得分:2)
您还需要在'*'
之前使用alaias名称SELECT row_number() over(ORDER BY data), t1.*
FROM
(
SELECT data
FROM myTable
ORDER BY data
) As t1
您不需要子查询来执行此操作,
SELECT data , row_number() over(ORDER BY data) as rn
FROM myTable
ORDER BY data