Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在BigQuery中选择CASE-LIKE_Sql_String_Google Bigquery_Case_Sql Like - Fatal编程技术网

Sql 如何在BigQuery中选择CASE-LIKE

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表达式的所有

您能帮助我们将这个伪代码翻译成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
表达式的所有分支必须返回相同的数据类型;似乎
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`