0

Oracle11g

列の並べ替え順序に関係なく、rownumを順番に(最小から最大に)表示したいと思います。これは、このクエリに示すように、副選択を介してクエリを送信することで実現できます。

質問:rownumの並べ替えを「orderby」句から独立させる他の方法はありますか?

with alphabet as
( select 'A' as letters from dual union all
  select 'B' as letters from dual union all 
  select 'C' as letters from dual)
  select rownum, letters from
  (select letters from alphabet
  --    order by  letters -- I can change the sort order here w/o changing rownum sort order.
     order by  letters desc)

副選択を行わない場合、列は次のようにソートされるため、rownumはソートされます。

with alphabet as
( select 'A' as letters from dual union all
  select 'B' as letters  from dual union all 
  select 'C' as letters from dual)
  select rownum, letters  from alphabet  
  order by letters desc      -- sorting here alters the rownum sort.
4

1 に答える 1

1

インラインビューを使用することの欠点は何ですか?

疑似列row_numberではなく、いつでも分析関数を使用できます。しかし、それはあなたが2つの異なる場所rownumにあなたを置くことを必要としますORDER BY

with alphabet as
( select 'A' as letters from dual union all
  select 'B' as letters  from dual union all
  select 'C' as letters from dual)
select row_number() over (order by letters desc) rn,
       letters
  from alphabet
 order by letters desc

これは、分析関数が外部クエリのとORDER BY一致する限り、並べ替え順序に関係なく機能しますORDER BY

SQL> ed
Wrote file afiedt.buf

  1  with alphabet as
  2  ( select 'A' as letters from dual union all
  3    select 'B' as letters  from dual union all
  4    select 'C' as letters from dual)
  5  select row_number() over (order by letters desc) rn,
  6         letters
  7    from alphabet
  8*  order by letters desc
SQL> /

        RN L
---------- -
         1 C
         2 B
         3 A

SQL> ed
Wrote file afiedt.buf

  1  with alphabet as
  2  ( select 'A' as letters from dual union all
  3    select 'B' as letters  from dual union all
  4    select 'C' as letters from dual)
  5  select row_number() over (order by letters asc) rn,
  6         letters
  7    from alphabet
  8*  order by letters asc
SQL> /

        RN L
---------- -
         1 A
         2 B
         3 C
于 2013-02-26T18:28:43.737 に答える