CASE WHEN flaged=1 THEN COALESCE(NULLIF(avg_purprice,0),NULLIF(purc_price,0),price) * quantity * COALESCE(1, NULLIF(k_mesure,0),1) ELSE (case when trantyp in (1,2,7) then sum_good when (trantyp=15 AND top_service=1) then (price * COALESCE(1, NULLIF(k_mesure,0),1)) ELSE (quantity * price) end) END