Активные артикулы по месяцам
SQL, по которому посчитано
WITH bounds AS (
SELECT date_trunc('month', max(date)) AS last_month
FROM sales
),
months AS (
SELECT DISTINCT date_trunc('month', date) AS month
FROM sales, bounds
WHERE date >= bounds.last_month - INTERVAL 11 MONTH
),
active AS (
SELECT date_trunc('month', s.date) AS month,
count(DISTINCT s.sku) AS active_skus
FROM sales s, bounds
WHERE s.date >= bounds.last_month - INTERVAL 11 MONTH
GROUP BY 1
)
SELECT m.month,
coalesce(a.active_skus, 0) AS active_skus
FROM months m
LEFT JOIN active a ON a.month = m.month
ORDER BY m.month