Take one table as input and output using another table BigQuery

被刻印的时光 ゝ 提交于 2021-01-28 12:14:49

问题


I have one table and want to use it as my input for a query pulling from another table:

input table:

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

And I'm using this query to use the input:

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

The issue is in the input table, if it's string, then it should be planning_category; if it's number, then it's item_name. what I think something could make sense is to change "where" clause above:

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

But this gives me error says "Semijoin expression (i.e. "x IN (SELECT ...)") must be a part of logical AND."

So is there any way to do this?

Thanks.


回答1:


These are all equivalent:

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))
)

The 3rd one works in BigQuery - but might produce some duplicates.

To prevent duplicates (1 row in this case):

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))
)

(replace (SELECT 'hamlet' x), (SELECT 'about' x) with your favorite table)




回答2:


The problem seems to be a SQL problem, not a bigquery one. As Turophile pointed out in his comment, the correct syntax should be

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



回答3:


I believe Google is not letting us use IN in ORs, only in ANDs. That's really awful, I'm facing the same problem.

The solution

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

will not solve the problem, and you'll get the same error Semijoin expression (i.e. "x IN (SELECT ...)") must be a part of logical AND.



来源:https://stackoverflow.com/questions/27932717/take-one-table-as-input-and-output-using-another-table-bigquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!