CREATE TABLE sales (
id int auto_increment primary key,
country VARCHAR(255),
sales_date DATE,
sales_volume INT,
fix_costs INT
);
INSERT INTO sales
(country, sales_date, sales_volume, fix_costs
)
VALUES
("DE", "2020-12-01", "500", "0"),
("NL", "2020-12-01", "320", "0"),
("FR", "2020-12-01", "350", "0"),
("DE", "2020-12-02", "700", "0"),
("NL", "2020-12-02", "420", "0"),
("FR", "2020-12-02", "180", "0"),
("DE", "2020-12-03", "0", "0"),
("NL", "2020-12-03", "0", "0"),
("FR", "2020-12-03", "0", "0"),
("None", "2020-12-31", "0", "2000");
Expected Result:
sales_date | country | sales_volume | fix_costs
-------------|--------------|------------------|------------------------------------------
2020-12-01 | DE | 500 | 37.95 (=2000/31 = 64.5 * 0.59)
2020-12-01 | FR | 350 | 26.57 (=2000/31 = 64.5 * 0.41)
2020-12-01 | NL | 320 | 0.00
-------------|--------------|------------------|------------------------------------------
2020-12-02 | DE | 700 | 51.32 (=2000/31 = 64.5 * 0.8)
2020-12-02 | FR | 180 | 13.20 (=2000/31 = 64.5 * 0.2)
2020-12-02 | NL | 420 | 0.00
-------------|--------------|------------------|------------------------------------------
2020-12-03 | DE | 0 | 32.26 (=2000/31 = 64.5 * 0.5)
2020-12-03 | FR | 0 | 32.26 (=2000/31 = 64.5 * 0.5)
2020-12-03 | NL | 0 | 0.00
-------------|--------------|------------------|-------------------------------------------
In the result above I want to split a fix_cost value per month (in this case 2000) to the country DE and FR per day based on the daily sales_volume in each of the selected countries. So far I have used the solution from this question which worked perfectly:
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)
END) AS fix_cost_per_day
FROM sales
GROUP BY 1,2;
However, as you can see in the data on 2020-12-03 there are no sales at all.
Therefore, the query above is not able to split the daily fix_costs to each country on this day.
To solve this issue I want to modify the query so in case there is a day without a sales_volume the values should be split 50/50.
How do I have to modify the query to achieve this?