在Apache的蜂巢我要表,我想離開,加入來自左數據保持所有數據和右表在可能添加數據。 爲此,我使用兩個連接,因爲連接基於兩個字段(一個material_id和一個location_id)。 這工作正常,有兩個傳統左聯接:蜂巢/ SQL - LEFT JOIN與後備
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
我們現在有要求,如果沒有「完美匹配「,這意味着只有material_id可以連接,並且對於b表中的location_id的連接,沒有material_id和location_id的正確組合(例如,material_id = 100和location_id = 1),連接應該爲」default 「或」後退「到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 ...;
一個設置中,我們嘗試了還是沒搞清楚到底是如何在蜂巢查詢語言做。
謝謝你的幫助!也許有人有一個聰明的想法。
下面是一些示例數據:
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:如上所述here存在等在子查詢ON不起作用。
我想,您需要向我們展示一些示例數據。 – Andrew
謝謝Andrew,爲了清晰起見,我添加了一些示例數據。 – alpcoder