简体   繁体   English

BigQuery - 标量子查询产生了不止一个元素

[英]BigQuery - Scalar subquery produced more than one element

I have the following data我有以下数据

LastSubmission          | PatientName | Phone   | SubmissionId | HealthCondition
2020-12-17 16:02:56 UTC |  a          |123456789| abc123       | Good
2020-12-18 14:24:33 UTC |  a          |123456789| abc123       | Bad
2020-12-18 14:24:51 UTC |  b          |523456789| def321       | okay
2020-12-18 14:25:09 UTC |  b          |523456789| def321       | bad
2020-12-21 17:11:40 UTC |  c          |623456789| hij987       | better
2020-12-21 17:05:30 UTC |  c          |623456789| hij981       | worse

I want to write a query that returns only the latest data for each SubmissionId我想编写一个只返回每个 SubmissionId 的最新数据的查询

Currently, I have the following code -目前,我有以下代码 -

SELECT *
FROM `myproject.dataset.qualtrics`
WHERE LastSubmission = 
( 
SELECT MAX(LastSubmission), 
FROM `myproject.dataset.qualtrics` 
GROUP BY SubmissionID, LastSubmission
) 
;

But when I run this I get an error saying 'Scalar subquery produced more than one element' Please help me solve this.但是当我运行它时,我收到一条错误消息,提示“标量子查询产生了多个元素”请帮我解决这个问题。

You want a correlated subquery:你想要一个相关的子查询:

SELECT q.*
FROM `myproject.dataset.qualtrics` q
WHERE LastSubmission = (SELECT MAX(q2.LastSubmission) 
                        FROM `myproject.dataset.qualtrics` q2
                        WHERE q2.SubmissionID = q.SubmissionID
                       ) ;

A more "bigquery"ish way to write the query would use aggregation:编写查询的一种更“大查询”的方式是使用聚合:

select array_agg(q order by q.LastSubmission desc limit 1)[ordinal(1)].*
from `myproject.dataset.qualtrics` q
group by q.SubmissionID;

暂无
暂无

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

相关问题 BigQuery JSON - 标量子查询产生了不止一个元素 - BigQuery JSON - Scalar subquery produced more than one element 导致错误的 BigQuery 搜索:标量子查询产生了多个元素 - BigQuery Search which results in Error: Scalar subquery produced more than one element 标量子查询产生了一个以上的元素 SQL,使用 SUM OVER PARTITION BY - Scalar subquery produced more than one element SQL, using SUM OVER PARTITION BY 如何使用正则表达式检测表中包含的文本(标量子查询产生多个元素) - How to detect a text contains in the table using regexp (scalar subquery produced more than one element) 除非使用 SELECT AS STRUCT 构建 STRUCT 值,否则标量子查询不能有超过一列 - Scalar subquery cannot have more than one column unless using SELECT AS STRUCT to build STRUCT values GCP 数据流批处理作业 - 防止工作人员在批处理作业中一次运行多个元素 - GCP Dataflow Batch jobs - Preventing workers from running more than one element at a time in a batch job BigQuery:以 UNION 作为 ARRAY 的子查询 - BigQuery: Subquery with UNION as ARRAY 添加对多个产品的订阅 - Adding a subscription to more than one product 如何创建一个新表,只保留Bigquery中相同ID下超过5条数据记录的行 - How to create a new table that only keeps rows with more than 5 data records under the same id in Bigquery 来自子查询的 BigQuery 日期分区条件 - BigQuery date partition condition from subquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM