繁体   English   中英

BigQuery:两组之间的对称差异(xor)

[英]BigQuery: Symmetric difference (xor) between two sets

BigQuery 有 UNION、INTERSECT 和 EXCEPT [1],但没有 XOR。

SELECT * FROM [0, 1,2,3] XOR SELECT * FROM [2,3,4]

将返回 0 1 4

因为 0 和 1 存在于第一个 select 但不是第二个,而 4 存在于第二个 select 但不是第一个。

我想用它来查找两个表之间的差异,例如查找存在于一个表中但不存在于其他表中的客户,反之亦然。

任何提示如何最好地做到这一点?

[1] https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#set_operators

BigQuery 不需要 XOR 运算符,因为它可以从现有运算符中获取:

@Genato 指出的第一种方法是在这个问题中使用JOIN

另一种方法是使用集合运算符:A XOR B 可以翻译为 (A AND NOT B) OR (B AND NOT A),因此对于您的示例,您可以编写

(
SELECT * FROM UNNEST(ARRAY<int64>[0, 1, 2, 3]) AS number
EXCEPT DISTINCT SELECT * FROM UNNEST(ARRAY<int64>[2, 3, 4]) AS number)

UNION ALL

(
    SELECT * FROM UNNEST(ARRAY<int64>[2,3,4]) AS number
EXCEPT DISTINCT SELECT * FROM UNNEST(ARRAY<int64>[0, 1, 2, 3]) AS number);

这导致:

在此处输入图像描述

很少有“解决方法”

选项1

with table1 as (
  select * from unnest([0, 1,2,3]) num
), table2 as (
  select * from unnest([2,3,4]) num
  )
select * from table1 where not num in (select num from table2)
union all 
select * from table2 where not num in (select num from table1)    

选项 2

with table1 as (
  select * from unnest([0,1,2,3]) num
), table2 as (
  select * from unnest([2,3,4]) num
)
select num from (
  select distinct num from table1 union all 
  select distinct num from table2 
)
group by num
having count(*) = 1    

在这两种情况下 - output 是

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM