CREATE MATERIALIZED VIEW insights.asset_distributions_sink_input_mv AS
SELECT
ad.asset_id,
CASE ad.dimension
WHEN 'ASSET_CLASS'
THEN 'asset_classes'
WHEN 'SECTOR'
THEN 'sectors'
WHEN 'GEOGRAPHY'
THEN 'regions'
WHEN 'CREDIT_QUALITY'
THEN 'credit_qualities'
WHEN 'CURRENCY'
THEN 'currencies'
END AS distribution_type,
ad.effective_start_date,
ad.effective_end_date,
JSONB_OBJECT_AGG(tn.code, ad.percentage) AS distribution,
JSONB_OBJECT_AGG(tn.code, ad.taxonomy_node_id) AS taxonomy_node_ids
FROM asset_service.asset_distributions_dm AS ad
JOIN asset_service.taxonomy_nodes_dm AS tn
ON tn.node_id = ad.taxonomy_node_id
WHERE
tn.disabled_at IS NULL
GROUP BY
ad.asset_id,
ad.dimension,
ad.effective_start_date,
ad.effective_end_date