case
を使用できます 式、両方とも周り 計算と内 ウィンドウのsum
:
select
sales_date,
country,
sum(sales_volume),
case when country <> 'NL'
then sum(sum(fix_costs)) over(partition by year(sales_date), month(sales_date))
/ day(last_day(sales_date))
* sum(sales_volume)
/ sum(case when country <> 'NL' then sum(sales_volume) else 0 end) over(partition by sales_date)
else 0
end as fix_cost_per_day
from sales
group by 1,2;
sales_date | country | sum(sales_volume) | fix_cost_per_day :--------- | :------ | ----------------: | ---------------: 2020-01-03 | DE | 500 | 37.95066414 2020-01-03 | FR | 350 | 26.56546490 2020-01-03 | NL | 320 | null 2020-01-30 | None | 0 | null 2020-02-15 | DE | 700 | 137.14733542 2020-02-15 | FR | 180 | 35.26645768 2020-02-15 | NL | 420 | null 2020-02-29 | None | 0 | null 2020-03-27 | DE | 180 | 20.19635344 2020-03-27 | FR | 970 | 108.83590463 2020-03-27 | NL | 670 | null 2020-03-31 | None | 0 | null