Доля себестоимости в выручке по артикулам и каналам за квартал
SQL, по которому посчитано
WITH sales_q AS (
SELECT
sku,
product,
channel,
sum(revenue - returned_revenue) AS net_revenue,
sum(cost_of_goods) AS cost
FROM sales
WHERE date >= (SELECT date_trunc('quarter', max(date)) FROM sales)
GROUP BY sku, product, channel
)
SELECT
sku || ' / ' || channel AS position,
product,
channel,
round(net_revenue) AS net_revenue,
round(cost) AS cost,
round(cost - net_revenue) AS cost_above_revenue,
round(cost / nullif(net_revenue, 0), 3) AS cost_share
FROM sales_q
ORDER BY cost_share DESC NULLS LAST
LIMIT 15