使用 window 函数和 case when 语句的另一种方法是什么

What is Another way to use division with window function and case when statement

我想计算特定时间内的每千次展示费用 (CPM),所以我使用了这个,效果很好,但还有其他更短的方法吗? 我使用 Window 函数显示所有行,不需要 GROUP BY

select      (sum(case when 
time_of_day_viewers_time_zone >= '06:00:00 - 06:59:59'
and time_of_day_viewers_time_zone <= '10:00:00 - 10:59:59'
then amount_spent_usd else null end) over ()/
sum(case when time_of_day_viewers_time_zone >= '06:00:00 - 06:59:59'
and time_of_day_viewers_time_zone <= '10:00:00 - 10:59:59'
then impressions else null end) over ())*1000 
as CPM
from t1

我用这个但是没用

select
case when time_of_day_viewers_time_zone >= '06:00:00 - 06:59:59'
and time_of_day_viewers_time_zone <= '10:00:00 - 10:59:59'
then (sum(amount_spent_usd) over ()/ sum(impressions) over ())*1000 
else null end as cpm
from t1

过滤聚合应该做到这一点:

(sum(amount_spent_usd) filter (where time_of_day_viewers_time_zone >= '06:00:00 - 06:59:59'
                                 and time_of_day_viewers_time_zone <= '10:00:00 - 10:59:59') over ())
                                  
/ 
   
((sum(impressions) filter (where time_of_day_viewers_time_zone >= '06:00:00 - 06:59:59'
                             and time_of_day_viewers_time_zone <= '10:00:00 - 10:59:59') over ()) * 1000)