简体   繁体   中英

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. 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).

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. Currently Bigquery returns "no rows", which is causing the main query to fail.

just simply add ARRAY in front of that subquery

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-2024 STACKOOM.COM