使用另一个表BigQuery将一个表作为输入和输出

时间:2015-01-13 22:32:11

标签: sql google-bigquery

我有一个表,并希望将其用作从另一个表中提取查询的输入:

输入表:

   +----------+--------+
    |   item   | period |
    +----------+--------+
    | HD.4TB   |      6 |
    | 12333445 |      7 |
    | 12344433 |      5 |
    +----------+--------+

我正在使用此查询来使用输入:

SELECT 
snapshot, 
item_name,  
commodity_code,
planning_category, 
type, 
SUM(quantity) qty, 
sdm_month_start_date,
FROM planning_extract 
WHERE 
planning_category IN (SELECT item FROM input) 
GROUP BY snapshot, 
item_name,  
commodity_code,
planning_category, 
type, 
sdm_month_start_date

问题出在输入表中,如果是字符串,那么它应该是planning_category;如果是数字,那么它是item_name。我认为有意义的是改变"其中"上述条款:

WHERE (planning_category or item_name) IN (SELECT item FROM input) 

但是这给了我错误说" Semijoin表达式(即" x IN(SELECT ...)")必须是逻辑AND的一部分。"

有没有办法做到这一点?

感谢。

3 个答案:

答案 0 :(得分:1)

这些都是等价的:

1

SELECT word, corpus FROM [publicdata:samples.shakespeare] 
WHERE (word OR corpus) IN (SELECT x FROM (SELECT 'hamlet' x), (SELECT 'about' x))

2

SELECT word, corpus FROM [publicdata:samples.shakespeare] 
WHERE word IN (SELECT x FROM (SELECT 'hamlet' x), (SELECT 'about' x))
OR corpus IN (SELECT x FROM (SELECT 'hamlet' x), (SELECT 'about' x))

3

SELECT word, corpus
FROM (
  SELECT word, corpus FROM [publicdata:samples.shakespeare] 
  WHERE word IN (SELECT x FROM (SELECT 'hamlet' x), (SELECT 'about' x))
), (
  SELECT word, corpus FROM [publicdata:samples.shakespeare] 
  WHERE corpus IN (SELECT x FROM (SELECT 'hamlet' x), (SELECT 'about' x))
)

第三个在BigQuery中运行 - 但可能会产生一些重复。

为了防止重复(在这种情况下为1行):

SELECT word, corpus
FROM (
  SELECT word, corpus FROM [publicdata:samples.shakespeare] 
  WHERE word IN (SELECT x FROM (SELECT 'hamlet' x), (SELECT 'about' x))
), (
  SELECT word, corpus FROM [publicdata:samples.shakespeare] 
  WHERE corpus IN (SELECT x FROM (SELECT 'hamlet' x), (SELECT 'about' x))
  AND word NOT IN (SELECT x FROM (SELECT 'hamlet' x), (SELECT 'about' x))
)

(用您最喜欢的表替换(SELECT'hamlet'x),(SELECT'about'x)

答案 1 :(得分:0)

这个问题似乎是一个SQL问题,而不是一个很大的问题。正如Turophile在他的评论中指出的那样,正确的语法应该是

WHERE (planning_category IN (SELECT item FROM input) OR item_name IN (SELECT item FROM input) )

答案 2 :(得分:0)

我相信Google不允许我们在IN中使用OR,只能在AND中使用WHERE (planning_category IN (SELECT item FROM input) OR item_name IN (SELECT item FROM input) ) 。那太可怕了,我面临着同样的问题。

解决方案

Semijoin expression (i.e. "x IN (SELECT ...)") must be a part of logical AND.

解决问题,您将收到同样的错误{{1}}