web-dev-qa-db-ja.com

Oracleデータベース-(COD)行ごとに2つの行(A、B)を取得します

次の例のように、Oracleデータベースと、Aの値とBの値を持つことができる変数があります。

cod   cat  v1   v2   v3   
123   A    1    10   11
123   B    0    0    0
111   A    2    3    4
111   B    0    7    0

テーブルに代金引換がありますRAP 2つの異なるテーブルに値v1、v2、またはv3がある場合、CATはAまたはBです。上の結果には、

ラップ:

id cod den
1  123 aaa
2  111 bbb

VAL1_A

fk_rap  v1  v2  v3
1       1   10  11
2       2   3   4

VAL1_B

fk_rap  v1  v2  v3
2       0   7   0

VAL1_Bに何もない場合に、catのBcodにゼロの行を取得するように、スクリプトを作成するにはどうすればよいですか。したがって、CATごとにCODAとBを取得する必要があります。値AまたはBはどこにもありません。これは、VAL1_AおよびVAL1_Bにある値に対してCATに入力するテキストです。

私はこのようなものを作っています:

        with cte as
            (select rap.cod,
                    case
                     when (coalesce(val1_a.v1,0)<>0 or
                           coalesce(val1_a.v2,0)<>0 or
                           coalesce(val1_a.v3,0)<>0)
                     then
                        'A'
                     when (coalesce(val1_b.v1,0)<>0 or
                           coalesce(val1_b.v2,0)<>0 or
                           coalesce(val1_b.v3,0)<>0)
                     then
                        'B'
                    end
                     as cat,
                   coalesce(val1_a.v1,0) as v1_a,
                   coalesce(val1_a.v2,0) as v2_a,
                   coalesce(val1_a.v3,0) as v3_a,
                   coalesce(val1_b.v1,0) as v1_b,
                   coalesce(val1_b.v2,0) as v2_b,
                   coalesce(val1_b.v3,0) as v3_b
             from rap
                 full outer join val1_a on val1_a.fk_rap=rap.id
                 full outer join val1_b on val1_b.fk_rap=rap.id
           )
     select cod,
            cat,
            case when cat='A' then v1_a
                 when cat='B' then v1_b
            end
              as v1,
            case when cat='A' then v2_a
                 when cat='B' then v2_b
            end
              as v2,
            case when cat='A' then v3_a
                 when cat='B' then v3_b
            end
              as v3
     from cte

しかし、AまたはBの値しかありません... Aの値とBの値が必要です...どうすればよいですか?

1
WDrgn

UNION ALLで解決します:

with cte as
        (select rap.cod,
                case
                 when (coalesce(val1_a.v1,0)<>0 or
                       coalesce(val1_a.v2,0)<>0 or
                       coalesce(val1_a.v3,0)<>0)
                 then
                    'A'
                end
                 as cat,
               coalesce(val1_a.v1,0) as v1_a,
               coalesce(val1_a.v2,0) as v2_a,
               coalesce(val1_a.v3,0) as v3_a,
               coalesce(val1_b.v1,0) as v1_b,
               coalesce(val1_b.v2,0) as v2_b,
               coalesce(val1_b.v3,0) as v3_b
         from rap
             full outer join val1_a on val1_a.fk_rap=rap.id
             full outer join val1_b on val1_b.fk_rap=rap.id
         UNION ALL
         select rap.cod,
                case
                 when (coalesce(val1_b.v1,0)<>0 or
                       coalesce(val1_b.v2,0)<>0 or
                       coalesce(val1_b.v3,0)<>0)
                 then
                    'B'
                end
                 as cat,
               coalesce(val1_a.v1,0) as v1_a,
               coalesce(val1_a.v2,0) as v2_a,
               coalesce(val1_a.v3,0) as v3_a,
               coalesce(val1_b.v1,0) as v1_b,
               coalesce(val1_b.v2,0) as v2_b,
               coalesce(val1_b.v3,0) as v3_b
         from rap
             full outer join val1_a on val1_a.fk_rap=rap.id
             full outer join val1_b on val1_b.fk_rap=rap.id
       )
 select cod,
        cat,
        case when cat='A' then v1_a
             when cat='B' then v1_b
        end
          as v1,
        case when cat='A' then v2_a
             when cat='B' then v2_b
        end
          as v2,
        case when cat='A' then v3_a
             when cat='B' then v3_b
        end
          as v3
 from cte
1
WDrgn