2016-07-29 55 views
1

我的表是这样的:ORDER BY语句 - 排序由多个列

A  B  C 
----------------------- 
111  3 
       777 
333  1 
555  2 
       333 
777  4 
888  5 

所以,我必须声明令“经B顺序”和我有结果是这样的:

A  B  C 
---------------------- 
333  1 
555  2 
111  3 
777  4 
888  5 
       777 
       333 

但是,我能做些什么来得到这个排序:

A  B  C 
----------------------- 
333  1 
       333 
555  2 
111  3 
777  4 
       777 
888  5 

如果列C时不为空,我应该排wher后把此行e A = C

谢谢!

所以,在这种情况下:

with a(a,b,c) as (select 111,4, null from dual union all 
        select null,null,777 from dual union all 
        select 333,1,null from dual union all 
        select 555,2, null from dual union all 
        select null,null, 333 from dual union all 
        select 777, 4, null from dual union all 

        select 444,null, 333 from dual union all 

        select 888, 5, null from dual union all 
        select null,null,777 from dual) 

select a.* 
    from a 
order by last_value(b ignore nulls) 
     over (partition by CASE when b is null then c else a end order by b), b nulls last 

我有输出(C 777是A 111后,因为B值是相同= 4):

A B  C 
--------------------   
333  1 
444   333 
      333 
555  2 
777  4 
111  4 
      777 
      777 
888  5 

但我要得到这样的:

A B  C 
    -------------------- 
    333  1 
    444   333 
       333 
    555  2 
    777  4 
       777 
       777 
    111  4 
    888  5 
+0

你可以有A和C不为空两行? – Aleksej

+0

是的,我可以有这样的行 – user2783755

+0

@ user2783755,请检查我的答案,有2个变种 –

回答

3

可能是这个帮助你:

with a(a,b,c) as (select 111,3, null from dual union all 
        select null,null,777 from dual union all 
        select 333,1,null from dual union all 
        select 555,2, null from dual union all 
        select null,null, 333 from dual union all 
        select 777, 4, null from dual union all 
        select 888, 5, null from dual) 

select a.* 
    from a 
order by last_value(b ignore nulls) over (partition by nvl(a,c) order by b), b nulls last 

输出

333 1 
     333 
555 2 
111 3 
777 4 
     777 
888 5 

7行中选择

或像你说的以后,你可以拥有两个不空A和C柱,你可以这样做:

with a(a,b,c) as (select 111,3, null from dual union all 
        select null,null,777 from dual union all 
        select 333,1,null from dual union all 
        select 555,2, null from dual union all 
        select null,null, 333 from dual union all 
        select 777, 4, null from dual union all 

        select 444,null, 333 from dual union all 

        select 888, 5, null from dual) 

select a.* 
    from a 
order by last_value(b ignore nulls) 
     over (partition by CASE when b is null then c else a end order by b), b nulls last 

输出

  A   B   C 
     333   1    
          333 
     444     333 
     555   2    
     111   3    
     777   4    
          777 
     888   5    

8 rows selected 
+0

我有一个额外的问题。如果在B列中可以是相同的值,例如所有的值都是2(其中A不为空),那么我想要有相同的顺序。谢谢! – user2783755

+0

您需要在附加案例中订购的逻辑是什么?如果b相同,给我举例数据并输出你想要的东西 –

+0

我已经添加了这个问题。感谢您的帮助。 – user2783755

1

做这样的:

select case when C in not null then C else A end as A,B,C from table 

它一样是这样的:

Declare @c nchar(50) 
Declare @a nchar(50) 
set @c = 'record' 
set @a = 'sample' 
select case when @c is not null then @c else @a end as A,@c as C 
+0

'当C in not null'无效SQL时。并且'declare @ c'对于Oracle也无效 –

+0

哦对不起o您认为您正在使用sql –

+0

@reds它是Sql,但rdbms是Oracle。 –