标量子查询产生了多个元素-标准SQL

时间:2019-01-30 15:18:14

标签: google-bigquery standard-sql

我的查询失败,并显示消息“标量子查询产生了多个元素”

  SELECT 
       EXTRACT (DATE FROM timestamp) AS Date
       ,jsonPayload.request.headers.origin AS URL
       ,jsonPayload.response.body.json.data.country.key AS Country
       ,(SELECT A.name FROM `pms_table_request_log.partners_request_log_*`, 
       UNNEST(jsonPayload.response.body.data.rows) AS A) AS Partner
       ,AVG((SELECT B.rating 
  FROM 
    `pms_table_request_log.partners_request_log_*`, 
     UNNEST(jsonPayload.response.body.data.rows) AS B)) AS Rating
    ,COUNT (*) AS Impression
    FROM 
       `pms_table_request_log.partners_request_log_*`
    GROUP BY 
        Date,URL,Country,Partner
    ORDER BY 
        Impression DESC;

请指导我做错了什么

1 个答案:

答案 0 :(得分:1)

您的子查询不是一个元素,而是一个以上的元素。当仅有一个元素的空间时,BQ不知道要处理多个元素。

您可以将结果馈入一个数组,该数组本身也只是一个元素。 您可以汇总或限制子查询的输出。 例如

(SELECT STRING_AGG(DISTINCT A.name) FROM `pms_table_request_log.partners_request_log_*`, 
       UNNEST(jsonPayload.response.body.data.rows) AS A) AS Partner

(SELECT AVG(B.rating) 
  FROM 
    `pms_table_request_log.partners_request_log_*`, 
     UNNEST(jsonPayload.response.body.data.rows) AS B)) AS Rating

作为汇总示例。

限制:

(SELECT A.name FROM `pms_table_request_log.partners_request_log_*`, 
       UNNEST(jsonPayload.response.body.data.rows) AS A LIMIT 1) AS Partner

不过,正确的选择取决于数据的含义。希望有帮助