如何在BigQuery中将CONCAT语句与CASE语句一起使用?

时间:2019-01-14 22:31:58

标签: google-bigquery case concat

在BigQuery中是否可以将CONCAT与CASE语句一起使用?如果是这样,怎么办?

当我们尝试这段代码时,它给出以下错误:

SELECT 

CONCAT(
     "Age:",
     CASE WHEN Age IS null THEN "" ELSE Age END AS Age
) AS AgeString

FROM [our table] 
  

错误:在第5行第49列遇到了““ AS”“ AS”“。期望:   “)” ...

2 个答案:

答案 0 :(得分:2)

作为一种选择-例如,如果出于某种原因别名对您来说“重要”,例如,在记录预期的和/或使代码更冗长/更具描述性的方面,则可以使用SELECT封装CASE,如下所示

#standardSQL
SELECT CONCAT(
  "Age:",
  (SELECT CASE WHEN Age IS NULL THEN "" ELSE Age END AS Age)
) AS AgeString
FROM `project.dataset.table`   

答案 1 :(得分:1)

问题在这里:

SELECT 

CONCAT(
     "Age:",
     CASE WHEN Age IS null THEN "" ELSE Age END AS Age  -- <- this alias
) AS AgeString

FROM [our table] 

由于您不能在表达式中放置别名,因此该查询使用旧版SQL或标准SQL返回错误。解决方法是删除别名:

SELECT 

CONCAT(
     "Age:",
     CASE WHEN Age IS null THEN "" ELSE Age END
) AS AgeString

FROM [our table]