Sql 过去7天内的分区平均值

Sql 过去7天内的分区平均值,sql,vertica,Sql,Vertica,我有一个跨越过去30天的查询,它是总收入的总和,但是我还想连同过去30天的总和,加上过去7天的平均值。我想要这样的东西: select country , avg(revenue) over (partition by country range between current_date - 7 and current_date) avg_revenue_last_7_days , sum(revenue) total_revenue_30_days from tab

我有一个跨越过去30天的查询,它是总收入的总和,但是我还想连同过去30天的总和,加上过去7天的平均值。我想要这样的东西:

select 
    country 
    , avg(revenue) over (partition by country range between current_date - 7 and current_date) avg_revenue_last_7_days
    , sum(revenue) total_revenue_30_days
from table 
group by 1,2
是否有可能获得比聚合所基于的天数更少的平均值


我想避免子查询,因为查询已经相当复杂了

这不需要窗口函数,只需要条件聚合:

select country,
       avg(case when datecol between current_date - 7 and current_date
                then revenue
           end) as avg_revenue_last_7_days,
       sum(case when datecol between current_date - 30 and current_date
                then revenue
           end) as total_revenue_30_days
from table 
group by country;

你怎么能按2分组?不要给不相关的产品贴标签。你应该提供样品数据和期望的结果。或者至少是您的数据布局。。。。我会添加
WHERE datecol>=CURRENT_DATE-30
,以最小化要分组的行。。。