--■■■名前でソートする方法■■■
with Work男 as (
select 名前,性別,Row_Number() over(order by 名前) as Rank
from 人
where 性別 = '男'),
Work女 as (
select 名前,性別,Row_Number() over(order by 名前) as Rank
from 人
where 性別 = '女')
select a.名前 as 男,b.名前 as 女,
coalesce(a.Rank,b.Rank) as Rank
from Work男 a full join Work女 b
on (a.Rank=b.Rank)
order by coalesce(a.Rank,b.Rank);
--■■■ランダムにソートする方法■■■
with Work男 as (
select 名前,性別,Row_Number() over(order by dbms_random.random()) as Rank
from 人
where 性別 = '男'),
Work女 as (
select 名前,性別,Row_Number() over(order by dbms_random.random()) as Rank
from 人
where 性別 = '女')
select a.名前 as 男,b.名前 as 女,
coalesce(a.Rank,b.Rank) as Rank
from Work男 a full join Work女 b
on (a.Rank=b.Rank)
order by coalesce(a.Rank,b.Rank);
--■■■model句を使う方法(10g以降)■■■
select 男,女,PID
from 人
model RETURN UPDATED ROWS
partition by (Row_number() over(partition by 性別 order by 名前) as PID)
DIMENSION BY (性別)
measures(名前 as 男,名前 as 女)
rules(
男['Wk'] = 男['男'],
女['Wk'] = 女['女']);