简体   繁体   中英

CASE WHEN error - setting value to column based on conditions from another column

I am trying write a case when statement which sets the country name to "United States" when the connection = "Snapchat". But I keep getting the error below:

No matching signature for operator CASE; all THEN/ELSE arguments must be coercible to a common type but found: BOOL, STRING; actual argument types (WHEN THEN) ELSE: (BOOL BOOL) STRING at [4:5]

SELECT 
    date,
    app_name,
    case when lower(connection) like '%snapchat%' then country_name = 'United States' else country_name end as country,
    connection,
    platform,
    ad_type,
    cast(Ad_Requests as FLOAT64) as ad_requests,
    cast(Ad_Impressions as FLOAT64) as ad_impressions,
    cast(Ad_Revenue____ as FLOAT64) as ad_revenue
FROM `dataset`

Any ideas how I can improve my query? Thank you in advance.

What you want looks like:

SELECT . . . 
       (case when lower(connection) like '%snapchat%'
             then 'United States' 
             else country_name
        end) as country,

Your formulation has a boolean result for the then clause. That would cause an error. But in addition, it references the column country which may not exist.

You can use below

SELECT 
    . . . 
    IF(lower(connection) like '%snapchat%', 'United States', country_name) as country,
    . . .
FROM `dataset`

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