简体   繁体   English

UNION ALL 参数化查询

[英]UNION ALL parameterised queries

I have a certain query which is working fine.我有一个工作正常的查询。 The problem is that a part of that query is a string that needs to be read from a file.问题是该查询的一部分是需要从文件中读取的字符串。 Query for each string produces 6 outputs.对每个字符串的查询产生 6 个输出。 I need a union of all the results for that file such that the end result is a table fo 6x number of strings.我需要该文件的所有结果的联合,以便最终结果是一个包含 6x 个字符串的表。 I can read the file using Python.我可以使用 Python 读取文件。

I've already tried using parameterised queries.我已经尝试过使用参数化查询。 Each of them only return the 6 rows based on the string.他们每个人只返回基于字符串的 6 行。

Most of my Python code is based on BigQuery's documentation here .我的大部分 Python 代码都基于此处的BigQuery 文档。

query = """
    SELECT pet_id, age, name
    FROM `myproject.mydataset.mytable`
    WHERE name = @name
    AND species = @species;
"""
query_params = [
    bigquery.ScalarQueryParameter('name', 'STRING', 'Max'),
    bigquery.ScalarQueryParameter('species', 'INT64', 'Dog'), 
    bigquery.ScalarQueryParameter('name', 'STRING', 'Alfred'), 
    bigquery.ScalarQueryParameter('species', 'INT64', 'Cat')
]
job_config = bigquery.QueryJobConfig()
job_config.query_parameters = query_params
query_job = client.query(
    query,
    # Location must match that of the dataset(s) referenced in the query.
    location='US',
    job_config=job_config)  # API request - starts the query

# Print the results
for row in query_job:
    print('{}: \t{}'.format(row.word, row.word_count))

How can I get a UNION ALL of many of these query results?如何获得这些查询结果的 UNION ALL?

The output should look like输出应该看起来像

pet_id | age | name
___________________
1      | 5   | Max
2      | 8   | Alfred

Please look at below example using public data (you can run the query as well)请查看以下使用公共数据的示例(您也可以运行查询)

#standardSQL
SELECT * 
FROM `bigquery-public-data.baseball.schedules`
WHERE (year, duration_minutes) IN UNNEST([(2016, 187), (2016, 165), (2016, 189)])

The key here is for you to provide an array of value that you want to filter the table with, and use IN UNNEST(array_of_values) to do the job, ideally like below:这里的关键是让您提供一个要用于过滤表的值数组,并使用IN UNNEST(array_of_values)来完成这项工作,理想情况如下:

query = """
    SELECT pet_id, age, name
    FROM `myproject.mydataset.mytable`
    WHERE (name, species) IN UNNEST(@filter_array);
"""

It is a bit unfortunate that BigQuery Python API doesn't let you specify array< struct<string, int64> > as query parameter.有点遗憾的是 BigQuery Python API 不允许您指定array< struct<string, int64> >作为查询参数。 So you may have to do:所以你可能必须这样做:

query = """
    SELECT pet_id, age, name
    FROM `myproject.mydataset.mytable`
    WHERE concat(name, "_", species) IN UNNEST(@filter_array);
"""
array_of_pre_concatenated_name_and_species = ['Max_Dog', 'Alfred_Cat']
query_params = [
    bigquery.ArrayQueryParameter('filter_array', 'STRING', array_of_pre_concatenated_name_and_species),
]

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

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