Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
1.1 kB
1
Indexable
Never
-- Предполагаем, что столбец source уже существует
SELECT
    transaction_date,
    agent_inn_dil,
    client_inn_dil,
    client_name_dil,
    client_name_GZPN,
    SUM(CASE WHEN label = 'dil_MO+KP' THEN total_monthly_ton_dil ELSE 0 END) OVER (PARTITION BY transaction_date, client_inn_dil) AS total_monthly_ton_dil,
    SUM(CASE WHEN label = 'dil_MO' AND source = 'MO' THEN total_monthly_ton_GZPN ELSE 0 END) AS MO_dealer_monthly_ton_GZPN,
    SUM(CASE WHEN label = 'dil_KP' AND source = 'KP' THEN total_monthly_ton_GZPN ELSE 0 END) AS KP_dealer_monthly_ton_GZPN,
    SUM(CASE WHEN label = 'dil_MO+KP' AND source = 'MO' THEN total_monthly_ton_GZPN ELSE 0 END) AS MO_dealer_monthly_ton_GZPN_for_MO_KP,
    SUM(CASE WHEN label = 'dil_MO+KP' AND source = 'KP' THEN total_monthly_ton_GZPN ELSE 0 END) AS KP_dealer_monthly_ton_GZPN_for_MO_KP,
    okved_client,
    label
FROM #dealer_MO_KP
GROUP BY transaction_date, agent_inn_dil, client_inn_dil, client_name_dil, client_name_GZPN, okved_client, label, source
ORDER BY client_inn_dil, transaction_date;
Leave a Comment