CREATE MATERIALIZED VIEW insights.benchmark_values_by_distribution_mv AS
SELECT
c.benchmark_id,
c.date AS fact_date,
ad.distribution_type,
ad.taxonomy_node_id,
ad.taxonomy_code,
SUM(c.weight * ad.share) AS weight
FROM asset_service.benchmark_constituents_ft AS c
JOIN insights.asset_distributions_for_consumers_mv AS ad
ON ad.asset_id = c.asset_id
AND c.date >= ad.effective_start_date
AND c.date < ad.effective_end_date
JOIN asset_service.benchmarks_dm AS b
ON b.id = c.benchmark_id
WHERE
b.disabled_at IS NULL
GROUP BY
c.benchmark_id,
c.date,
ad.distribution_type,
ad.taxonomy_node_id,
ad.taxonomy_code