简短:有没有办法在不存在的 BQ 字段中进行查询,并为这些字段接收空值?
我几乎遇到了BigQuery IF 字段存在 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>)
Run Code Online (Sandbox Code Playgroud)
有没有办法做这样的事情:
SELECT IFEXISTS(a, NULL) as the-field
FROM <somewhere w/o my_field>
Run Code Online (Sandbox Code Playgroud)
假设您的表只有 x 和 y 字段!
所以下面的查询将完美地工作
SELECT x, y FROM YourTable
Run Code Online (Sandbox Code Playgroud)
但是由于不存在的字段 z,低于 1 将失败
SELECT x, y, z FROM YourTable
Run Code Online (Sandbox Code Playgroud)
解决方法如下
#legacySQL
SELECT x, y, COALESCE(z, 0) as z
FROM
(SELECT * FROM YourTable),
(SELECT true AS fake, NULL as z)
WHERE fake IS NULL
Run Code Online (Sandbox Code Playgroud)
编辑:添加显式
#legacySQL以避免混淆那些试图将这种确切方法应用于标准 SQL 的人:o)
这可以通过使用脚本来完成:
DECLARE my_field STRING;
SET my_field = "default";
-- my_field falls back to "default" if there is no such column in my_table
SELECT my_field FROM my_table;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5664 次 |
| 最近记录: |