SELECT
    Week,
    SUM(cube_tac) / SUM_IF(cube_revenue_w_cpa, fraud_type = 'normal') AS Tac_Rate_All,
    SUM(cube_tac) / SUM_IF(cube_revenue_w_cpa, fraud_type = 'normal' AND commerce_type = 'commerce') AS Tac_Rate_Commerce,
    SUM_IF(cube_tac, segment IN ('sdk', 'ssp')) / SUM_IF(cube_revenue_w_cpa, fraud_type = 'normal' AND commerce_type = 'commerce' AND segment IN ('sdk', 'ssp')) AS Tac_Rate_InApp,
    SUM_IF(cube_tac, traffic_type IN ('search')) / SUM_IF(cube_revenue_w_cpa, fraud_type = 'normal' AND commerce_type = 'commerce' AND traffic_type IN ('search')) AS Tac_Rate_Search
FROM hahn.`home/mcsanalytics/kalamkas/yan_cube_shortened_v1`
WHERE 1 = 1
AND ownership_type = 'external'
GROUP BY Week