简短:有没有办法在不存在的 BQ 字段中进行查询,并为这些字段接收空值?
我有几乎同样的问题
BigQuery IF field exists THEN但有时我的 API 可以查询没有某些特定字段(历史表)的表,这种方法失败,因为它需要一个包含该字段的表:
SELECT a, b, c, COALESCE(my_field, 0) as my_field
FROM
(SELECT * FROM <somewhere w/o my_field>),
(SELECT * FROM <somewhere with my_field>)
有没有办法做这样的事情:SELECT IFEXISTS(a, NULL) as the-field
FROM <somewhere w/o my_field>
最佳答案
假设您的表只有 x 和 y 字段!
所以下面的查询将完美地工作
SELECT x, y FROM YourTable
但由于不存在的字段 z,低于 1 将失败
SELECT x, y, z FROM YourTable
解决方法如下
#legacySQL
SELECT x, y, COALESCE(z, 0) as z
FROM
(SELECT * FROM YourTable),
(SELECT true AS fake, NULL as z)
WHERE fake IS NULL
EDIT: added explicit
#legacySQL
to not to confuse those who is trying to apply this exact approach to Standard SQL :o)
关于google-bigquery - Bigquery 如果字段存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40096320/