简体   繁体   中英

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

I have 500 distinct values in one column. I would like to know how to split if possible in 2 columns.

Of course I can use case in select something like this

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;

but this divide the column in two but the number of rows persists. there will be in column A nulls in 251-500 and in column B 1-250 respectivly.

but I need 250 rows as result with first 250 values in columns A and next 250 values in column B

thanks

You can use aggregation. Here is one method that puts this as:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM