0

テーブルの結合から値を取得するために使用する、結合ステートメントを使用した選択ステートメントクエリがあります

ここに私の選択クエリがあります:

select 
case when p1.Value2 is not null then p1.Value3 end as fix1,
case when p1.Value2 is not null then p2.Value3 end as fix2,
case when p1.Value2 is not null then p3.Value3 end as fix3,
case when p1.Value2 is not null then p4.Value3 end as fix4,
case when p1.Value2 is not null then p5.Value3 end as fix5,
case when p1.Value2 is not null then p6.Value3 end as fix6
from tc t
left join (select * from  Parameter where paramtype='fix') as p1 on p1.Value2 = t.FIX 
left join (select * from  Parameter where paramtype='fix') as p2 on p2.Value2 = t.FIX1
left join (select * from  Parameter where paramtype='fix') as p3 on p3.Value2 = t.FIX2
left join (select * from  Parameter where paramtype='fix') as p4 on p4.Value2 = t.FIX3
left join (select * from  Parameter where paramtype='fix') as p5 on p5.Value2 = t.FIX4
left join (select * from  Parameter where paramtype='fix') as p6 on p6.Value2 = t.FIX5
where CUST_ACCT ='10053996'

複雑だと思ったので、コードを次のように変更します。

select 
fix +' '+(select Value3 from Parameter where ParamType = 'fix' and Value2 = fix) as fix,
FIX1 +' '+(select Value3 from Parameter where ParamType = 'fix' and Value2 = FIX1) as FIX1,
FIX2 +' '+(select Value3 from Parameter where ParamType = 'fix' and Value2 = FIX2) as FIX2,
FIX3 +' '+(select Value3 from Parameter where ParamType = 'fix' and Value2 = FIX3) as FIX3,
FIX4 +' '+(select Value3 from Parameter where ParamType = 'fix' and Value2 = FIX4) as FIX4,
FIX5 +' '+(select Value3 from Parameter where ParamType = 'fix' and Value2 = FIX5) as FIX5
from tc where CUST_ACCT ='10053996'

私のコードを簡素化することは可能ですか? または、このようなことをしたい場合は、そのような選択ステートメントを作成する必要がありますか?

4

4 に答える 4

3

そのような解決策はどうですか?

SELECT /*extra fields from tc ==>>*/ x, y /*<<==*/
    , FIX, FIX1, FIX2, FIX3, FIX4, FIX5
FROM (
    SELECT fields, P.Value3, /*extra fields from tc ==>>*/ x, y /*<<==*/
    FROM (
        SELECT *
        FROM tc
        WHERE CUST_ACCT ='10053996'
    ) T
    UNPIVOT (
        unpvt FOR fields in (FIX,FIX1,FIX2,FIX3,FIX4,FIX5)
    ) UPV
    LEFT JOIN Parameter P ON P.paramtype='fix' AND P.Value2 = UPV.unpvt
) T
PIVOT (
    MIN(Value3) FOR fields in (FIX,FIX1,FIX2,FIX3,FIX4,FIX5)
) AS PV
于 2013-11-13T08:29:34.940 に答える
1

同じテーブルに何度も参加する必要はまったくありません。これを回避するための簡単なトリックは次のとおりです。

select 
case when pt.f = 1 then pt.Value3 end as fix1,
case when pt.f2 = 1 then pt.Value3 end as fix2,
... 
from tc
left join (select 
      CASE WHEN p.Value2 = t.FIX THEN 1 ELSE 0 END AS f, 
      CASE WHEN p.Value2 = t.FIX1 THEN 1 ELSE 0 END AS f1, 
      CASE WHEN p.Value2 = t.FIX2 THEN 1 ELSE 0 END AS f2, 
      CASE WHEN p.Value2 = t.FIX3 THEN 1 ELSE 0 END AS f3, 
      CASE WHEN p.Value2 = t.FIX4 THEN 1 ELSE 0 END AS f4, 
      CASE WHEN p.Value2 = t.FIX5 THEN 1 ELSE 0 END AS f5
    from  Parameter AS p where paramtype = 'fix') AS pt
    ON pt.Value2 IN (tc.FIX, tc.FIX2, tc.FIX3, tc.FIX4, tc.FIX5)
where CUST_ACCT ='10053996'

おそらく、JOIN具体的な状況に応じて条件を単純化できます。アイデアは、複数の結合の代わりにパラメーターを導入することです。

于 2013-11-13T08:30:30.163 に答える
0

どうですか:

select
case when p1.Value2 is not null then p1.Value3 end as fix1,
case when p1.Value2 is not null then p2.Value3 end as fix2, 
case when p1.Value2 is not null then p3.Value3 end as fix3, 
case when p1.Value2 is not null then p4.Value3 end as fix4, 
case when p1.Value2 is not null then p5.Value3 end as fix5, 
case when p1.Value2 is not null then p6.Value3 end as fix6 
from tc t 
left join Parameter as p1 
on p1.Value2 = t.FIX  AND p1.paramtype='fix'  
left join Parameter as p2 
on p2.Value2 = t.FIX1 AND p2.paramtype='fix'  
left join Parameter as p3
on p3.Value2 = t.FIX2 AND p3.paramtype='fix' 
left join Parameter as p4
on p4.Value2 = t.FIX3 AND p4.paramtype='fix' 
left join Parameter as p5
on p5.Value2 = t.FIX4 AND p5.paramtype='fix' 
left join Parameter as p6
on p6.Value2 = t.FIX5 AND p6.paramtype='fix' 
where CUST_ACCT ='10053996'
于 2013-11-13T11:17:20.017 に答える