トップページに戻る
次のSQLパズルへ
前のSQLパズルへ
8-43 Oracle9iでnocycleを模倣
SQLパズル
Table
COLA COLB
---- ----
1 2
2 3
3 4
4 5
5 1
6 7
7 8
nocycleを使った下記のクエリを
Oracle9iで模倣する。
select ColA,ColB
from (select 1 as ColA,2 as ColB from dual
union select 2 as ColA,3 as ColB from dual
union select 3 as ColA,4 as ColB from dual
union select 4 as ColA,5 as ColB from dual
union select 5 as ColA,1 as ColB from dual
union select 6 as ColA,7 as ColB from dual
union select 7 as ColA,8 as ColB from dual)
start with ColA = 1
connect by nocycle prior ColB = ColA;
出力結果
COLA COLB
---- ----
1 2
2 3
3 4
4 5
5 1
SQL
select distinct ColA,ColB
from (select 1 as ColA,2 as ColB from dual
union select 2 as ColA,3 as ColB from dual
union select 3 as ColA,4 as ColB from dual
union select 4 as ColA,5 as ColB from dual
union select 5 as ColA,1 as ColB from dual
union select 6 as ColA,7 as ColB from dual
union select 7 as ColA,8 as ColB from dual)
start with ColA = 1
connect by prior ColB = ColA
and Level != 50
and prior dbms_random.value != 1;
解説