如何解决Bigquery:基于不同的 date_trunc 多次运行查询并联合结果,而不是多个 UNION ALL
我希望从核心表中提取和合并类似的聚合,但在时间段截断上有所不同。例如,我通常会这样做:
with base as (
select
event_datetime,event_location,event_uuid
from events_table
)
select
date_trunc(event_datetime,day) as day,"day" as period_type,count(distinct event_uuid) as number_events
from base
group by day,event_location
union all
select
date_trunc(event_datetime,week) as week,"week" as period_type,count(distinct event_uuid) as number_events
from base
group by week,week) as month,"month" as period_type,count(distinct event_uuid) as number_events
from base
group by month,event_location
有谁知道是否有办法避免维护三个单独的子查询并有一个基于不同的 date_trunc 重新运行的子查询,并结合结果(产生与上面代码相同的输出)?我知道我以前的公司有人做到了这一点,但我不知道是如何做到的。
谢谢!
解决方法
考虑以下方法
select
day,period_type,event_location,count(distinct event_uuid) as number_events
from base t,unnest([
struct('day' as period_type,date_trunc(event_datetime,day) as day),struct('week',week)),struct('month',month))
])
group by day,event_location
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。