2016-07-29 95 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。 –