简体   繁体   中英

SQL in BigQuery CASE WHEN with categorical variables

I need to redistribute my revenue depending on the platform source (PC, Web, Mobile) because I pay a different tax for any of those platforms in order to get my.net revenue in Google BigQuery. This is a sample of my data:

在此处输入图像描述

I have tried to create a CASE WHEN depending on the values of the platform source but it is not working. The output is only zeros.

CASE WHEN ${platform_source} = 'Mobile' THEN ${revenue_raw} * 0.7
              WHEN ${platform_source} = 'Web' THEN ${revenue_raw} * 0.88
              WHEN ${platform_source} = 'Pc' THEN ${d1_iap_revenue_raw} * 0.88
              ELSE NULL END

Thanks a lot!

String matching is case sensitive.

Try this

CASE WHEN ${platform_source} = 'Mobile' THEN ${revenue_raw} * 0.7
              WHEN ${platform_source} = 'Web' THEN ${revenue_raw} * 0.88
              WHEN ${platform_source} = 'PC' THEN ${d1_iap_revenue_raw} * 0.88
              ELSE NULL END

If none of the conditions match, as @Mr.Batra has mentioned, there is a possibility of the Platform column containing extra spaces. If the string lengths are not the expected values, the column has to be processed.

To process the column and remove any trailing or leading spaces, use the TRIM function.

CASE WHEN TRIM(${platform_source}) = 'Mobile' THEN ${revenue_raw} * 0.7
     WHEN TRIM(${platform_source}) = 'Web' THEN ${revenue_raw} * 0.88
     WHEN TRIM(${platform_source}) = 'PC' THEN ${d1_iap_revenue_raw} * 0.88
ELSE NULL END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM