Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
793 B
2
Indexable
Never
SELECT
    client_inn_dil,
    SUM(CASE WHEN label = 'dil_MO' THEN total_monthly_ton_GZPN ELSE 0 END) AS MO_dealer_monthly_ton_GZPN,
    SUM(CASE WHEN label = 'dil_KP' THEN total_monthly_ton_GZPN ELSE 0 END) AS KP_dealer_monthly_ton_GZPN
INTO #temp_aggregated_values
FROM #dealer_MO_KP_modified
GROUP BY client_inn_dil;

-- Обновляем значения в #dealer_MO_KP_modified для клиентов с 'dil_MO+KP'
UPDATE m
SET
    m.MO_dealer_monthly_ton_GZPN = t.MO_dealer_monthly_ton_GZPN,
    m.KP_dealer_monthly_ton_GZPN = t.KP_dealer_monthly_ton_GZPN,
    m.MO_KP_monthly_ton_GZPN = t.MO_dealer_monthly_ton_GZPN + t.KP_dealer_monthly_ton_GZPN
FROM #dealer_MO_KP_modified AS m
JOIN #temp_aggregated_values AS t ON m.client_inn_dil = t.client_inn_dil
WHERE m.label = 'dil_MO+KP';
Leave a Comment