数据有若干记录,其中主键列为: 








我要按照一定的格式取出来,如下的 
1,2 
3,4 
5,6 
7,8 



要求,需要纯用Select式的SQL实现,不用存储过程和函数等,如何?

解决方案 »

  1.   

    select (case when rownum % 2 = 1 then id else null end)||','||(case when rownum % 2 = 0 then id else null end) 
    from (select * from tablename order by id);
      

  2.   

    select t01||','||t02
    from 
    (
           select  t01,lead(t01)over(order by 1) t02 from t
    )
    where mod(t01,2)=1
      

  3.   

    SELECT wmsys.wm_concat(id) str 
      FROM (
               SELECT rownum id, round(rownum/2) id2  
                 FROM user_objects 
                WHERE rownum<=20)
     GROUP BY id2
      

  4.   


    drop table test;
    create table test(id number);begin
        for i in 1..20
        loop
            insert into test values(i);
        end loop;
        commit;
    end;
    /select a.id || ',' || b.id as id
      from (select t.*, rownum as rid from (select id from (select t.*, rownum as rid from test t order by id) where mod(rid, 2) = 1) t) a,
           (select t.*, rownum as rid from (select id from (select t.*, rownum as rid from test t order by id) where mod(rid, 2) = 0) t) b
     where a.rid = b.rid;ID
    -------------1,2
    3,4
    5,6
    7,8
    9,10
    11,12
    13,14
    15,16
    17,18
    19,20
      

  5.   

    select max(a)||','||max(b) new_col from(
    select case when mod(rownum,2)=1 then col end a,
           case when mod(rownum,2)=0 then col end b
    from tab1)
    group by trunc((rownum-1)/2)
    order by new_col
      

  6.   

    with t as (
    select 1 pkid from dual union
    select 2 from dual union
    select 3 from dual union
    select 4 from dual union
    select 5 from dual union
    select 6 from dual union
    select 7 from dual)
    select wmsys.wm_concat(pkid) f1
      from (SELECT pkid,ceil(rn/2) rn1
              FROM (select pkid,rownum rn from t))
     group by rn1
      

  7.   

    with t as (
    select 1 pkid from dual union
    select 2 from dual union
    select 3 from dual union
    select 4 from dual union
    select 5 from dual union
    select 6 from dual union
    select 7 from dual)
    select wmsys.wm_concat(pkid) f1
      from (select pkid,rownum rn from t)
     group by ceil(rn/2)
      

  8.   

    wildwave这位大哥给弄出来了,结果是正确的,非常的感谢