Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

sql - 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...