简体   繁体   English

Bigquery 标量子查询产生了多个元素

[英]Bigquery Scalar subquery produced more than one element

I have a scalar subquery that looks up city name based on a city code and state code.我有一个标量子查询,它根据城市代码和 state 代码查找城市名称。 Sometimes the subquery does not return a match and this is causing an error: 'Scalar subquery produced more than one element'.有时子查询不返回匹配项,这会导致错误:“标量子查询产生了多个元素”。 I verified that there are no duplicates in the lookup table (city code + state code is unique).我验证了查找表中没有重复项(城市代码 + state 代码是唯一的)。

WITH city_names AS (
    SELECT  city_abbr,state_code,city_name
FROM  `myproject.mydataset.city_lookup_table`
  )

SELECT  
consumerId, 
(SELECT city_name from city_names WHERE city_names.city_abbr = city_code AND city_names.state_code = state_code) AS cityName_new 

FROM (
 SELECT consumerId, city_code,state_code
FROM `myproject.mydataset.Customers` 
)

I want the subquery to return city_name = NULL if there are no matching city_abbr and state_id.如果没有匹配的 city_abbr 和 state_id,我希望子查询返回 city_name = NULL。 Currently Bigquery returns "no rows", which is causing the main query to fail.目前 Bigquery 返回“无行”,这导致主查询失败。

just simply add ARRAY in front of that subquery只需在该子查询前面添加 ARRAY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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