BigQuery收到相关子查询错误-获取上一个事件

时间:2019-03-13 22:41:03

标签: database google-bigquery

我有2个BigQuery表:

1-次旅行

car     start                   end    
---------------------------------------------------
1       2019-03-13T17:07:00     2019-03-13T17:17:00
2       2019-03-13T17:07:00     2019-03-13T17:22:00
3       2019-03-13T17:07:00     2019-03-13T17:34:00
4       2019-03-13T17:07:00     2019-03-13T17:12:00

2-跟踪

car     created_at              status  
--------------------------------------
1       2019-03-13T17:01:00     1
1       2019-03-13T17:02:00     1
1       2019-03-13T17:03:00     1
1       2019-03-13T17:04:00     1
1       2019-03-13T17:05:00     2
1       2019-03-13T17:06:00     2
1       2019-03-13T17:18:00     3
1       2019-03-13T17:19:00     3
1       2019-03-13T17:20:00     3
1       2019-03-13T17:21:00     3
1       2019-03-13T17:22:00     3

跟踪表包含直到旅行的汽车的状态。我的目标是在旅行的前一刻获得汽车的状态。

到目前为止,我的方法是:

select *,
(select status created_at from tracking
  where car = tracking.car
  AND start > created_at
  order by created_at desc
  limit 1
) as previous_status
from trips

但是我遇到以下错误:

Correlated subqueries that reference other tables are not supported unless they can be de-correlated, such as by transforming them into an efficient JOIN.

关于如何重写BigQuery查询的任何线索吗?

1 个答案:

答案 0 :(得分:0)

要获取上一个事件,请使用php artisan view: clear,如下所示:

LAG() OVER()

enter image description here