如何做不平等加入BigQuery?

时间:2014-10-21 12:31:17

标签: google-bigquery

我希望在<中使用JOIN,但会收到错误:

  

错误:ON子句必须为AND =每个表中一个字段名称的比较,所有字段名称都以表名为前缀。

查询:

select
  o1.order_number,
  o1.datetime,
  LAST(o2.datetime),

from spree.orders_unique as o1
join spree.orders_unique as o2
  on o1.order_number = o2.order_number
    and o1.datetime < o2.datetime
group by o1.order_number, o1.datetime

我将如何做到这一点?

1 个答案:

答案 0 :(得分:4)

您可以在此处投票/加注此问题:https://code.google.com/p/google-bigquery/issues/detail?id=39

您需要使用CROSS JOIN + WHERE子句来运行不等式

select
  o1.order_number,
  o1.datetime,
  LAST(o2.datetime),

from spree.orders_unique as o1
CROSS JOIN spree.orders_unique as o2
where o1.order_number = o2.order_number
    and o1.datetime < o2.datetime
group by o1.order_number, o1.datetime

注意如果此查询资源耗尽,您可能需要使用CROSS JOIN EACH