Oracle: Display row number with 'order by' clause

前端 未结 4 1126
独厮守ぢ
独厮守ぢ 2021-02-02 17:14

I wonder how could i print a row number for sql statement where is using order. Currently i tried ROWNUM but as i understand it works only for unsorted result set.

4条回答
  •  遇见更好的自我
    2021-02-02 17:40

    In addition to nesting the query, you can use an analytic function

    SELECT row_number() OVER (ORDER BY a.full_name),
           lg_id,
           full_name,
           sort_order
      FROM activity_type_lang a
     WHERE a.lg_id = 'en'
     ORDER BY a.full_name
    

    Using analytic functions also makes it easier if you want to change how ties are handled. You can replace ROW_NUMBER with RANK or DENSE_RANK.

提交回复
热议问题