在 Oracle 中将一列中的不同 500 个值拆分为 2 列,每列 250 个值

Split distinct 500 values in one column into 2 columns of 250 values each in Oracle

我在一列中有 500 个不同的值。我想知道如何拆分成 2 列。

当然我可以在 select 中使用 case,像这样

SELECT 
case when rownum between 1 and 250 then i.item end a,
case when rownum between 251 and 500 then i.item end b
from items i;

但这将列一分为二,但行数仍然存在。 A 列 251-500 和 B 列 1-250 分别会有空值。

但我需要 250 行作为结果,前 250 个值在 A 列中,接下来的 250 个值在 B 列中

谢谢

您可以使用聚合。这是一种将其表示为的方法:

1     2
3     4
. . .

select min(case when mod(seqnum, 2) = 0 then item end),
       min(case when mod(seqnum, 2) = 1 then item end)       
from (select i.*, rownum - 1 as seqnum
      from items i
     ) i
group by floor(seqnum / 2)