这有点复杂,但您可以按如下方式进行:
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
这样做是为每列中的每个不同值分配一个序列号。然后,它将这些组合到每个序列号的单行中。该组合使用union all
/group by
方法。另一种配方使用full outer join
.
此版本使用id
列将值保持在与它们在原始数据中出现的顺序相同的顺序。
在 H2 中(最初不是问题),您可以改用该rownum()
函数(在此处记录)。但是,您可能无法指定排序。