如何在Google Big Query中实现MINUS运算符

Tej*_*eja 6 sql google-bigquery

我正在尝试在Google Big Query中实现MINUS操作,但看起来查询参考中没有文档.有人可以分享你对此的看法.我以前在常规SQL中完成了它,但不确定Google是否在Big Query中提供它.您的意见得到赞赏.谢谢.

Mat*_*ley 10

只是在此处添加更新,因为这篇文章仍然出现在 Google 搜索中。BigQuery 现在支持 EXCEPT 集合运算符。

https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax# except

select * from t1
EXCEPT DISTINCT
select * from t2;
Run Code Online (Sandbox Code Playgroud)


Gor*_*off 4

如果 BigQuery 不提供minusexcept,您可以使用 执行相同的操作not exists

select t1.*
from table1 t1
where not exists (select 1
                  from table2 t2
                  where t2.col1 = t1.col1 and t2.col2 = t1.col2 . . .
                 );
Run Code Online (Sandbox Code Playgroud)

这对于非 NULL 值可以正确工作。为了NULL价值观,你需要多一点努力。并且,这也可以写成left join

select t1.*
from table1 t1 left join
     table2 t2
     on t2.col1 = t1.col1 and t2.col2 = t1.col2
where t2.col1 is null;
Run Code Online (Sandbox Code Playgroud)

其中之一应该是 bigquery 可以接受的。

  • BigQuery 还不支持 EXISTS,因此它仅是 LEFT JOIN。 (3认同)