Apache Hive では、左側のデータからすべてのデータを保持し、可能な場合は右側のテーブルからデータを追加して、左結合したいテーブルを作成する必要があります。結合は 2 つのフィールド (material_id と location_id) に基づいているため、このために 2 つの結合を使用します。これは、従来の 2 つの左結合で問題なく機能します。
SELECT
a.*,
b.*
FROM a
INNER JOIN (some more complex select) b
ON a.material_id=b.material_id
AND a.location_id=b.location_id;
location_id の場合、データベースには 1 と 2 などの 2 つの異なる値しか含まれていません。
「完全一致」がない場合、これは、material_id のみを結合でき、location_id の結合に material_id と location_id の正しい組み合わせ (例: material_id=100 と location_id=1) がないことを意味するという要件があります。 b テーブルでは、結合は location_id の他の可能な値 (例: material_id=001 と location_id=2) に「デフォルト」または「フォールバック」する必要があります。これは、location_id の場合にのみ当てはまります。
CASE などについても可能な限りの回答を検討してきましたが、うまくいきませんでした。みたいなセットアップ
...
ON a.material_id=b.material_id AND a.location_id=
CASE WHEN a.location_id = b.location_id THEN b.location_id ELSE ...;
私たちは、Hive クエリ言語で実際にどのように実行するかを試みましたが、理解できませんでした。
ご協力ありがとうございました!誰かが賢い考えを持っているかもしれません。
サンプルデータは次のとおりです。
Table a
| material_id | location_id | other_column_a |
| 100 | 1 | 45 |
| 101 | 1 | 45 |
| 103 | 1 | 45 |
| 103 | 2 | 45 |
Table b
| material_id | location_id | other_column_b |
| 100 | 1 | 66 |
| 102 | 1 | 76 |
| 103 | 2 | 88 |
Left - Join Table
| material_id | location_id | other_column_a | other_column_b
| 100 | 1 | 45 | 66
| 101 | 1 | 45 | NULL (mat. not in b)
| 103 | 1 | 45 | DEFAULT TO where location_id=2 (88)
| 103 | 2 | 45 | 88
PS:ここで述べたように、存在するなどは、サブクエリ ON では機能しません。