当情况下使用分区

时间:2019-06-09 21:39:08

标签: sql amazon-redshift window-functions

总结我要做什么:

我有很多地区,其中包含很多产品。我已经计算出每种产品的折扣,产品的平均折扣(按区域划分)以及标准差(也按区域划分)。

现在,我需要计算新的平均折扣(再次按地区划分),但仅考虑 折扣

select  product,
        discount,
        region,
--this is the line i want to add
        avg (case when discont < avg_discount + stddev_discount then discount) over(partition by region) end as new_discount*
from (
  select product,
         discount,
         region,
         avg(discount) over (partition by region) as avg_discount,
         stddev(discount) over (partition by region) as stddev_discount
  from base
)

我想要什么:

what i want:

1 个答案:

答案 0 :(得分:0)

我不清楚您想要什么结果。对于您所描述的,聚合似乎足以获得最终结果。

无论如何,我们的想法是使用子查询或CTE。  像这样:

select region, avg(discount)
from (select t.*,
             avg(discount) over (partition by region) as region_avg,
             stddev(discount) over (partition by region) as region_stddev
      from t
     ) t
where discount < region_avg + region_stdev
group by region;

编辑:

您可以调整它以使用窗口功能:

select t.*,
       avg(case when discount < region_avg + region_stdev then discount end) over (region) as new_avg
from (select t.*,
             avg(discount) over (partition by region) as region_avg,
             stddev(discount) over (partition by region) as region_stddev
      from t
     ) t;