4

データをランダムに並べ替える最良の方法は何ですか? 例:

id col1    col2     col3
1  data11  data12   data13
2  data21  data22   data23
3  data31  data32   data33

col1 と col2 のデータをランダムに並べ替えると、結果は次のようになります。

id col1    col2     col3
1  data31  data22   data13
2  data11  data12   data23
3  data21  data32   data33
4

1 に答える 1

7

DBMS_RANDOM を使用してランダム性を実現します。サブクエリの ROW_NUMBER() は、WHERE 句で使用できるフックを提供します。それ以外の場合は、CROSS JOIN を取得します (これはあまりランダムではなく、巨大になる可能性があります)。

with c1 as ( select col1
                , row_number() over (order by dbms_random.value ) rn
             from your_table )
     , c2 as ( select col2 
                , row_number() over (order by dbms_random.value ) rn
             from your_table )
     , c3 as ( select col3 
                , row_number() over (order by dbms_random.value ) rn
             from your_table )
select c1.col1
       , c2.col2
       , c3.col3
from c1, c2, c3
where c2.rn = c1.rn
and   c3.rn = c1.rn;

最良の結果を得るには、シードを使用することを忘れないでください。 詳細をご覧ください

于 2013-03-11T13:53:49.533 に答える