有没有办法在连接子句中执行 If Then 语句
Is there a way to do an If Then statement in the join clause
我有两个表,spend 和 bookings,我正在尝试对其进行完全连接。由于表格的性质(并非所有预订都有支出,也并非所有支出都有 booking_id,有时会分配错误的 booking_id),我想在加入子句。
select bk.booking_id, bk.company_id, bk.event_id, bk.total_amount_booked, sp.spend
from
(select booking_id, company_id, event_id, booked_rate, total_amount_booked
from bookings) bk
full join
(select booking_id, company_id, event_id, normalized_spend, spend
from spend) sp
on (bk.booking_id = sp.booking_id and bk.booked_rate = sp.normalized_spend)
or (bk.company_id = sp.company_id and bk.event_id = sp.event_id);
基本上,如果 booking_id 匹配且标准化支出与 booked_rate 匹配,则在该条件下加入。
如果没有,请加入 company_id 和 event_id。
但是,执行此查询 returns
FULL JOIN is only supported with merge-joinable join conditions;
有更好的方法吗?
使用 Redshift
这有点麻烦,但您可以用两个 left joins
和 union all
模拟 full
连接:
select bk.booking_id, bk.company_id, bk.event_id, bk.total_amount_booked, sp.spend
from bookings bk
left join spend sp
on (bk.booking_id = sp.booking_id and bk.booked_rate = sp.normalized_spend)
or (bk.company_id = sp.company_id and bk.event_id = sp.event_id)
union all
select bk.booking_id, bk.company_id, bk.event_id, bk.total_amount_booked, sp.spend
from spend sp
left join bookings bk
on (bk.booking_id = sp.booking_id and bk.booked_rate = sp.normalized_spend)
or (bk.company_id = sp.company_id and bk.event_id = sp.event_id)
where bk.booking_id is null
也许使用 CASE 语句,例如
...ON CASE WHEN (bk.booking_id = sp.booking_id and bk.booked_rate = sp.normalized_spend)
THEN (bk.booking_id = sp.booking_id and bk.booked_rate = sp.normalized_spend)
WHEN (bk.company_id = sp.company_id and bk.event_id = sp.event_id)
THEN (bk.company_id = sp.company_id and bk.event_id = sp.event_id)
END
我有两个表,spend 和 bookings,我正在尝试对其进行完全连接。由于表格的性质(并非所有预订都有支出,也并非所有支出都有 booking_id,有时会分配错误的 booking_id),我想在加入子句。
select bk.booking_id, bk.company_id, bk.event_id, bk.total_amount_booked, sp.spend
from
(select booking_id, company_id, event_id, booked_rate, total_amount_booked
from bookings) bk
full join
(select booking_id, company_id, event_id, normalized_spend, spend
from spend) sp
on (bk.booking_id = sp.booking_id and bk.booked_rate = sp.normalized_spend)
or (bk.company_id = sp.company_id and bk.event_id = sp.event_id);
基本上,如果 booking_id 匹配且标准化支出与 booked_rate 匹配,则在该条件下加入。 如果没有,请加入 company_id 和 event_id。
但是,执行此查询 returns
FULL JOIN is only supported with merge-joinable join conditions;
有更好的方法吗? 使用 Redshift
这有点麻烦,但您可以用两个 left joins
和 union all
模拟 full
连接:
select bk.booking_id, bk.company_id, bk.event_id, bk.total_amount_booked, sp.spend
from bookings bk
left join spend sp
on (bk.booking_id = sp.booking_id and bk.booked_rate = sp.normalized_spend)
or (bk.company_id = sp.company_id and bk.event_id = sp.event_id)
union all
select bk.booking_id, bk.company_id, bk.event_id, bk.total_amount_booked, sp.spend
from spend sp
left join bookings bk
on (bk.booking_id = sp.booking_id and bk.booked_rate = sp.normalized_spend)
or (bk.company_id = sp.company_id and bk.event_id = sp.event_id)
where bk.booking_id is null
也许使用 CASE 语句,例如
...ON CASE WHEN (bk.booking_id = sp.booking_id and bk.booked_rate = sp.normalized_spend)
THEN (bk.booking_id = sp.booking_id and bk.booked_rate = sp.normalized_spend)
WHEN (bk.company_id = sp.company_id and bk.event_id = sp.event_id)
THEN (bk.company_id = sp.company_id and bk.event_id = sp.event_id)
END