如何解决根据 when 语句中的不同日期参数多次运行查询并合并结果,而不是多个 UNION ALL
我希望从核心表中提取和合并类似的聚合,但在 WHEN 语句中定义的日期期间有所不同。我通常会这样做:
with base as (
select
event_datetime,event_location,event_uuid
from events_table
)
select
"last_30_days" as period,count(distinct event_uuid) as number_events
from base
where event_datetime >= current_date - 30
group by event_location
union all
select
"last_60_days" as period,count(distinct event_uuid) as number_events
from base
where event_datetime >= current_date - 60
group by event_location
union all
select
"all_time" as period,count(distinct event_uuid) as number_events
from base
where event_datetime >= current_date - 10000
group by event_location
有谁知道是否有办法避免必须维护三个单独的子查询,并让一个子查询根据不同的时间段重新运行并合并结果(产生与上述代码相同的输出)?
解决方法
考虑以下方法
select
period,event_location,count(distinct if(event_datetime >= current_date - days,event_uuid,null)) as number_events
from base,unnest([
struct('last_30_days' as period,30 as days),struct('last_60_days',60),struct('all_time',10000)
])
group by period,event_location
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。