0

ID の複数のコードと値を持つ特性テーブルと、各コードと値に対応する説明を持つルックアップ テーブルがあります。ID、descr1、descr2 を 2 つのテーブルから選択したいと考えています。最初の descr は 1 つのルックアップ コードと値のペア用であり、descr は別のペア用です。例えば:

Table1
ID   Code    Value
1    Color   1
1    Tone    4
1    Type    Poor
2    Color   3
2    Tone    4


Table2
Code   Value  Descr
Color  1      Red
Color  2      Blue
Color  3      Yellow
Tone   4      Clear
Type   Good   Used, but good condition
Type   New    New
Type   Poor   Used, poor condition

ID 1 を照会して色とタイプを取得できるようにしたいので、次のようなレコードを取得します

ID   Color   Type
1    Red     Used, poor condition

そのうちの 1 つを取得できますが、同じ行で 2 番目の取得に失敗しています

select t1.ID, t2.Descr as Color
from Table1 t1
join Table2 t2
  on t1.Code = t2.Code
 and t1.Value = t2.Value
where t1.ID = 1
and t1.Code = (select t2b.Code
                 from Table2 t2b
                where t1.Code = t2b.Code
                  and t1.Value = t2b.Value
                  and t1.Value = 'Color')

私はそれについてすべて間違っていると思います、そして私は探してきました-この質問はすでに尋ねられていると確信していますが、私はそれを見つけていません. 実行しようとしていることに関するヘルプを見つけるために、クエリの種類で使用されている単語を知る必要がある場合があります。

更新 GKV と knagaev からの回答を組み合わせました。max と case を組み合わせることで、探していたものが得られたためです。これは私が欲しかったものを私に与えました:

select t1.ID
      ,max((case when t1.Code='Color' then t2.Descr else null end)) as Color
      ,max((case when t1.Code='Type' then t2.Descr else null end)) as Type
from Table1 t1,Table2 t2
where t1.Code = t2.Code and t1.Value = t2.Value
  and   t1.ID = 1
group by t1.ID
4

2 に答える 2

4

単純な結合だけで、文字列の連結とともに仕事ができます。ややこのように

  select t1.ID
    ,wm_concat(case when t1.Code='Color' then t2.Descr else null end) as Color
    ,wm_concat(case when t1.Code='Type' then t2.Descr else null end) as Type
    from Table1 t1,Table2 t2
    where t1.Code = t2.Code and t1.Value = t2.Value
      and   t1.ID = 1
    group by t1.ID
于 2012-12-19T03:24:08.680 に答える
2

「標準」のOracle SQLのみを使用する

select t1.id,
  max(decode (t1.code, 'Color', t2.descr, null)) color,
  max(decode (t1.code, 'Tone', t2.descr, null)) tone,
  max(decode (t1.code, 'Type', t2.descr, null)) type
from table1 t1, table2 t2
where t1.code = t2.code
  and t1.value = t2.value
  and t1.id = 1
group by t1.id

SQLフィドル

于 2012-12-19T09:07:55.640 に答える