Sql 如何在BigQuery中选择CASE-LIKE
您能帮助我们将这个伪代码翻译成BigQuery吗Sql 如何在BigQuery中选择CASE-LIKE,sql,string,google-bigquery,case,sql-like,Sql,String,Google Bigquery,Case,Sql Like,您能帮助我们将这个伪代码翻译成BigQuery吗 select case product_id when like 'a%' then -1 when product_id like 'b%' then -2 else product_id end as product_id from `some.table.name` 你就快到了。基本上,您需要使用长格式的case,其中在每个分支中重复条件;您正在使用的缩写形式仅支持相等条件 另一个问题是case表达式的所有
select case product_id when like 'a%' then -1
when product_id like 'b%' then -2
else product_id end as product_id
from `some.table.name`
你就快到了。基本上,您需要使用长格式的
case
,其中在每个分支中重复条件;您正在使用的缩写形式仅支持相等条件
另一个问题是case
表达式的所有分支必须返回相同的数据类型;似乎product\u id
是一个字符串,因此需要返回字符串而不是数字,以与else
分支保持一致
因此:
另一个选项(BigQuery标准SQL)使用“short”形式的case,如下例所示
#standardSQL
select
case substr(product_id, 1, 1)
when 'a' then '-1'
when 'b' then '-2'
else product_id
end as product_id
from `some.table.name`
#standardSQL
select
case substr(product_id, 1, 1)
when 'a' then '-1'
when 'b' then '-2'
else product_id
end as product_id
from `some.table.name`