我在BigQuery上有以下SQL查询。我正在尝试加入两个不同的表,其中一个表比另一个小得多。最初,我使用了常规联接,但这会消除一些正在使用的数据。
with weekly_periods as(
select
ticket_id,
start_time_in_minutes_from_week,
raw_delta_in_minutes,
week_number,
greatest(0, start_time_in_minutes_from_week - week_number * (7 * 24 * 60)) as ticket_week_start_time,
least(start_time_in_minutes_from_week + raw_delta_in_minutes - week_number * (7 * 24 * 60),(7 * 24 * 60)) as ticket_week_end_time
from
ticket_solved_time,
unnest(generate_array(0, floor((start_time_in_minutes_from_week + raw_delta_in_minutes) / (7 * 24 * 60)), 1)) as week_number,
intercepted_periods as(
select
ticket_id,
week_number,
ticket_week_start_time,
ticket_week_end_time,
schedule.start_time as schedule_start_time,
schedule.end_time as schedule_end_time,
least(ticket_week_end_time, schedule.end_time) - greatest(ticket_week_start_time, schedule.start_time) as scheduled_minutes
from
weekly_periods
left join
schedule
on ticket_week_start_time <= schedule.end_time
and ticket_week_end_time >= schedule.start_time
但是我收到以下错误:-如果没有连接两侧的字段相等的条件,则不能使用LEFT OUTER JOIN-
在保留数据的同时如何进行此联接?如果我只是做JOIN查询不会返回完整的结果。 谢谢!
答案 0 :(得分:3)
您需要做的是使用交叉联接,然后在where子句中添加联接条件,如下所示:
with weekly_periods as(
select
ticket_id,
start_time_in_minutes_from_week,
raw_delta_in_minutes,
week_number,
greatest(0, start_time_in_minutes_from_week - week_number * (7 * 24 * 60)) as ticket_week_start_time,
least(start_time_in_minutes_from_week + raw_delta_in_minutes - week_number * (7 * 24 * 60),(7 * 24 * 60)) as ticket_week_end_time
from
ticket_solved_time,
unnest(generate_array(0, floor((start_time_in_minutes_from_week + raw_delta_in_minutes) / (7 * 24 * 60)), 1)) as week_number,
intercepted_periods as(
select
ticket_id,
week_number,
ticket_week_start_time,
ticket_week_end_time,
schedule.start_time as schedule_start_time,
schedule.end_time as schedule_end_time,
least(ticket_week_end_time, schedule.end_time) - greatest(ticket_week_start_time, schedule.start_time) as scheduled_minutes
from
weekly_periods
Cross join
schedule
Where ticket_week_start_time <= schedule.end_time
and ticket_week_end_time >= schedule.start_time
答案 1 :(得分:1)
您需要使用=
运算符(下面的示例)基于关系键联接表,然后使用where
来实现您的条件...由于您没有发布表结构,这只是正确加入方式的一个例子
left join
schedule
on schedule.id = weekly_period.ticketid and
weekly_period.ticketid = intercepted_period.ticketid
where
ticket_week_start_time <= schedule.end_time
and ticket_week_end_time >= schedule.start_time