表testid
1
2
3
4
5
6
7
8
9
10需要得到如下的结果:
单数   双数
1    2
3    4
5    6
7    8
9    10 我写的sql语句如下:
select
case when mod(id,2)=1 then id end 单数,
case when mod(id,2)=0 then id end 双数
from test;      单数       双数
---------- ----------
         1
                    2
         3
                    4
         5
                    6
         7
                    8
         9
                   10我想问下null行是怎么产生的,如何才能得到我要的结果?

解决方案 »

  1.   

    select max(单数)单数,max(双数)双数 from(    
    select 
    case when mod(id,2)=1 then id end 单数, 
    case when mod(id,2)=0 then id end 双数,
    row_number(partition by mod(id,2) order by id)rn
    from test
      )
    group by rn  
    order by rn
      

  2.   

    没有聚合,查询前后的记录数当然是一样多的
    经过case
    产生空值
      

  3.   

    。。打错了
    select max(单数)单数,max(双数)双数 from(    
    select 
    case when mod(id,2)=1 then id end 单数, 
    case when mod(id,2)=0 then id end 双数,
    row_number(partition by mod(id,2) order by id)rn
    from test
      )
    group by rn  
    order by rn
      

  4.   


    你写错了,row_number() over()
    根据你的方法,我要的效果达到了,不过我还是想知道为什么我写的sql语句会出现null的数据,可否帮忙解释下
      

  5.   

    你的case里,满足条件的输出id,else 没写,则默认为null
    这就是空值的来历
      

  6.   

    还有就是表连接了
    select a.单数,b.双数 from
    (select id 单数,rownum rn from test where mod(id,2)=1)a
    full join
    (select id 双数,rownum rn from test where mod(id,2)=0)b
    on a.rn=b.rn
      

  7.   


    这个不错,我觉得使用left join更明了一些