これは少し複雑ですが、次のように実行できます。
select max(thing) as thing, max(other) as other, max(stuff) as stuff
from ((select row_number() over (order by id) as seqnum, thing, NULL as other, NULL as stuff
from (select thing, min(id) as id from t group by thing
) t
) union all
(select row_number() over (order by id) as seqnum, NULL, other, NULL
from (select other, min(id) as id from t group by other
) t
) union all
(select row_number() over (order by id) as seqnum, NULL, NULL, stuff
from (select stuff, min(id) as id from t group by stuff
) t
)
) t
group by seqnum
これは、各列の個別の値にシーケンス番号を割り当てることです。次に、これらを組み合わせて、シーケンス番号ごとに1つの行にします。組み合わせはunion all
/group by
アプローチを使用します。別の定式化では、を使用しfull outer join
ます。
このバージョンでは、id
列を使用して、元のデータに表示されるのと同じ順序で値を保持します。
H2(元々は質問に含まれていませんでした)では、rownum()
代わりに関数を使用できます(ここに記載されています)。ただし、順序を指定できない場合があります。