如果 BigQuery 中没有要显示的数据,如何 return 空值
How to return null values if there is no data to display in BigQuery
我的问题是,当 BigQuery 中“没有要显示的数据”时,我想获取空值。
像这样:
但是只有聚合函数才有效。如何修改以下查询以便 returns 空值?
我的查询:
select oid, date, coalesce(sum(quantity_sold),0) as quantity_sold
from table
where oid = 'xxx' and (date >= 'xxx' and date <= 'xxx')
group by 1,2
我发现这与 相似,但它创建了一个包含“未找到结果”消息的列,并将空值分配给其他列。您可以应用此查询并删除消息并仅保留空值,您的查询将如下所示:
with sample_data as (
select 123 as oid, '2022-01-01' as date, 23 as quantity_sold
union all select 111 as oid, '2022-01-02' as date, 24 as quantity_sold
),
actual_query as (
select oid,date,coalesce(sum(quantity_sold),0) as quantity_sold
from sample_data
where oid = 534 and (date >= '2021-03-23' and date <= '2021-04-23')
group by 1,2
)
-- query below is the modified query from the linked SO question above
select actual_query.*
from actual_query
union all
select actual_query.* -- all the `actual_query` columns will be `NULL`
from (select 1) left join
actual_query
on 1 = 0 -- always false
where not exists (select 1 from actual_query);
示例输出:
注意:我为示例数据创建了 运行dom 值,当我 运行 您的查询时,这些值可以模仿消息“没有要显示的数据”。
我的问题是,当 BigQuery 中“没有要显示的数据”时,我想获取空值。
像这样:
但是只有聚合函数才有效。如何修改以下查询以便 returns 空值?
我的查询:
select oid, date, coalesce(sum(quantity_sold),0) as quantity_sold
from table
where oid = 'xxx' and (date >= 'xxx' and date <= 'xxx')
group by 1,2
我发现这与
with sample_data as (
select 123 as oid, '2022-01-01' as date, 23 as quantity_sold
union all select 111 as oid, '2022-01-02' as date, 24 as quantity_sold
),
actual_query as (
select oid,date,coalesce(sum(quantity_sold),0) as quantity_sold
from sample_data
where oid = 534 and (date >= '2021-03-23' and date <= '2021-04-23')
group by 1,2
)
-- query below is the modified query from the linked SO question above
select actual_query.*
from actual_query
union all
select actual_query.* -- all the `actual_query` columns will be `NULL`
from (select 1) left join
actual_query
on 1 = 0 -- always false
where not exists (select 1 from actual_query);
示例输出:
注意:我为示例数据创建了 运行dom 值,当我 运行 您的查询时,这些值可以模仿消息“没有要显示的数据”。