我檢查了多個問題,但無法指出與我相似的主題。如果這看起來重復,請指向鏈接。
基本上我有2張桌子。我需要加入 2 個表,然后如果它不為空,則使用來自 table_b 的 store_nr。如果它為空,則使用 id_nr 表格 table_a。我可以使用 union 和 join 來實作這一點,但它效率不高,因為表很大。尋找任何更快的解決方案。
SELECT a.month,
a.id_nr
from table_a a
where a.id_nr not in (select distinct to_char(b.group_id) from table_b)
union
SELECT a.month,
CASE
WHEN b.store_nr is not null
then b.store_nr
ELSE to_number(a.id_nr)
END id_nr
FROM table_a a
join table_b b
on a.id_nr = to_char(b.group_id)
uj5u.com熱心網友回復:
我認為你真的是在你的 table_b 到你的 table_a 的左外連接之后,加上 aCOALESCE來處理要顯示的列,比如:
select a.month,
coalesce(b.store_nr, a.id_nr) id_nr
from table_a a
left outer join table_b b on a.id_nr = to_char(b.group_id);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/518043.html
標籤:sql甲骨文
上一篇:Oracle-允許空值的唯一約束
