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