2016-07-29 21 views
1

複数の列によって順序付け:だから注文 - 私はこのようなテーブルを持っている

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

、私は「Bによるため、」ORDER BY文を持っていると私はこのようになります:

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 nullではない、私は行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 

Iが出力される(C 777が原因B値の、111の後にある同じ= 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 nullではないの両方を持つ行を持つことができますか? – 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行後で、あなたは両方を持つことができますNOT NULL 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がnullでない場合)の場合、同じ順序付けが必要です。ありがとう! – 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

ああ、申し訳ありませんが、あなたはSQLで作業していると思っています –

+0

@redsこれはSQLですが、rdbmsはOracleです。 –

関連する問題