BigQuery比较两个列表。

huangapple go评论63阅读模式
英文:

BigQuery compare two lists

问题

DECLARE list ARRAY ;
DECLARE list2 ARRAY ;

SET list1 = ['orange','red','black' ];
SET list2 = ['red','black' ];

作为结果,我想要获得'orange',因为它是在list2中缺失的值。

有人能帮我吗?

我尝试过这个解决方案,但是太慢了。我的列表实际上包含数百个值,所以我想要比在每个值前添加'as value union all select'更高效的方法。

WITH list1 AS (
  SELECT 'orange' AS value UNION ALL
  SELECT 'red' AS value UNION ALL
  SELECT 'black' AS value
)
,
list2 AS (
  SELECT 'red' AS value UNION ALL
  SELECT 'black' AS value 
)

SELECT value
FROM (
  SELECT value, 'list1' AS list_name
  FROM list1
  UNION ALL
  SELECT value, 'list2' AS list_name
  FROM list2
)
GROUP BY value
HAVING COUNT(*) = 1
ORDER BY value
英文:

I would like to compare two lists of strings using BigQuery.

DECLARE list ARRAY <String>;
DECLARE list2 ARRAY <String>;

SET list1 = ['orange','red','black' ];
SET list2 = ['red','black' ];

AS a result, i would like to get 'orange' as it is the value missing in list2.

Can anyone help me please ?

I've tried this solution, but it takes too long. My lists actually contain hundreds of values, so I'd like something more efficient than adding 'as value union all select' in front of each value.

WITH list1 AS (
  SELECT &#39;orange&#39; AS value UNION ALL
  SELECT &#39;red&#39; AS value UNION ALL
  SELECT &#39;black&#39; AS value
)
,
list2 AS (
  SELECT &#39;red&#39; AS value UNION ALL
  SELECT &#39;black&#39; AS value 
)

SELECT value
FROM (
  SELECT value, &#39;list1&#39; AS list_name
  FROM list1
  UNION ALL
  SELECT value, &#39;list2&#39; AS list_name
  FROM list2
)
GROUP BY value
HAVING COUNT(*) = 1
ORDER BY value

答案1

得分: 1

以下是您提供的代码的中文翻译:

考虑以下(BigQuery标准SQL)

DECLARE list1, list2 ARRAY<string>;
SET list1 = ['orange','red','black' ]; SET list2 = ['red','black' ];
SELECT * FROM (
  SELECT DISTINCT el FROM UNNEST(list1) el
  UNION ALL
  SELECT DISTINCT el FROM UNNEST(list2) el
)
GROUP BY el
HAVING COUNT(*) = 1;

带有输出

BigQuery比较两个列表。

还请考虑以下方法

DECLARE list1, list2 ARRAY<string>;
SET list1 = ['orange','red','black', 'green' ]; SET list2 = ['red','black', 'pink' ];
SELECT list, ARRAY_AGG(el) el FROM (
  SELECT el, MIN(list) list FROM (
    SELECT DISTINCT el, 'list1' list FROM UNNEST(list1) el
    UNION ALL
    SELECT DISTINCT el, 'list2' FROM UNNEST(list2) el
  )
  GROUP BY el
  HAVING COUNT(*) = 1
)
GROUP BY list;

带有输出

BigQuery比较两个列表。

英文:

Consider below (BigQuery Standard SQL)

DECLARE list1, list2 ARRAY&lt;string&gt;;
SET list1 = [&#39;orange&#39;,&#39;red&#39;,&#39;black&#39; ]; SET list2 = [&#39;red&#39;,&#39;black&#39; ];
SELECT * FROM (
  SELECT DISTINCT el FROM UNNEST(list1) el
  UNION ALL
  SELECT DISTINCT el FROM UNNEST(list2) el
)
GROUP BY el
HAVING COUNT(*) = 1;    

with output

BigQuery比较两个列表。

Also consider below approach

DECLARE list1, list2 ARRAY&lt;string&gt;;
SET list1 = [&#39;orange&#39;,&#39;red&#39;,&#39;black&#39;, &#39;green&#39; ]; SET list2 = [&#39;red&#39;,&#39;black&#39;, &#39;pink&#39; ];
SELECT list, ARRAY_AGG(el) el FROM (
  SELECT el, MIN(list) list FROM (
    SELECT DISTINCT el, &#39;list1&#39; list FROM UNNEST(list1) el
    UNION ALL
    SELECT DISTINCT el, &#39;list2&#39; FROM UNNEST(list2) el
  )
  GROUP BY el
  HAVING COUNT(*) = 1
)
GROUP BY list

with output

BigQuery比较两个列表。

答案2

得分: 0

DECLARE list1, list2 ARRAY;
SET list1 = ['orange', 'red', 'black'];
SET list2 = ['red', 'black'];
SELECT *
FROM (
SELECT *
FROM UNNEST(list1) as list1
)
WHERE list1 not in (
SELECT * FROM UNNEST(list2)
)

英文:

A little variation of the previous answer, not sure it will be faster then the previous code, given that your lists contain 100+ values.

DECLARE list1, list2 ARRAY&lt;string&gt;;
SET list1 = [&#39;orange&#39;,&#39;red&#39;,&#39;black&#39; ]; SET list2 = [&#39;red&#39;,&#39;black&#39; ];
SELECT *
FROM (
  SELECT *
  FROM UNNEST(list1) as list1
)
WHERE list1 not in (
  SELECT * FROM UNNEST(list2)
)

huangapple
  • 本文由 发表于 2023年7月4日 22:28:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定