简体   繁体   English

查询“有效”但产生错误:标量子查询产生多个元素

[英]Query “is valid” but produces an error: Scalar subquery produced more than one element

This query “is valid” according to the BigQuery SQL editor. 根据BigQuery SQL编辑器,此查询“有效”。 However when it is run, it produces an error:Scalar subquery produced more than one element 但是,当它运行时会产生一个错误:标量子查询产生了多个元素

Input: 输入:

SELECT 
  (Select 
     pcr.repdte 
     from 
     usa_fdic_call_reports_1992.All_Reports_19921231_
     Performance_and_Condition_Ratios as PCR) as Quarter,
(SELECT 
     Round(PCR.lnlsdepr) 
     FROM 
   usa_fdic_call_reports_1992.All_Reports_19921231_Performance_
   and_Condition_Ratios as PCR) as NetLoansAndLeasesToDeposits,
(SELECT LD.IDdepsam 
    FROM usa_fdic_call_reports_1992.All_Reports_19921231_
      Deposits_Based_on_the_dollar250_000_Reporting_Threshold 
      AS LD) as DepositAccountsWithMoreThan250k

Output Scalar subquery produced more than one element 输出标量子查询产生了多个元素

在此处输入图片说明

The output of the queries when they are run independently is below: 当它们独立运行时,查询的输出如下:

SELECT
  PCR.repdte as quarter
  FROM 
     usa_fdic_call_reports_1992.All_Reports_19921231_
     Performance_and_Condition_Ratios as PCR 

Output: 输出: 在此处输入图片说明

SELECT 
  Round(PCR.lnlsdepr) as NetLoansAndLeasesToDeposits
  FROM 
     usa_fdic_call_reports_1992.All_Reports_19921231_
     Performance_and_Condition_Ratios as PCR

Output: 输出: 在此处输入图片说明

SELECT LD.IDdepsam as DepositAccountsWithMoreThan250k
  FROM 
    usa_fdic_call_reports_1992.All_Reports_
    19921231_Deposits_Based_on_the_dollar250_000_
     Reporting_Threshold AS LD 

Output: 输出: 在此处输入图片说明

Scalar subqueries cannot produce more than a single row . 标量子查询最多只能产生一行 You are showing your scalar subqueries show a single column , and multiple rows . 您正在显示标量子查询显示单列多行 That -- by definition -- won't work. 从定义上来说,那是行不通的。

I solved the problem by not using a subquery, and instead using JOIN 我通过不使用子查询,而是使用JOIN解决了问题

SELECT 
  pcr.cert as cert,
  pcr.name as NameOfBank,
  pcr.repdte as Quarter, 
  Round(PCR.lnlsdepr) as NetLoansAndLeasesToDeposits,
  LD.IDdepsam as DepositAccountsWithMoreThan250k
FROM 
    usa_fdic_call_reports_1992.All_Reports_19921231_Performance
    _and_Condition_Ratios as PCR
JOIN
    usa_fdic_call_reports_1992.All_Reports_19921231_Deposits_Based_
    on_the_dollar250_000_Reporting_Threshold AS LD
ON PCR.cert = LD.cert

Output: 输出: 在此处输入图片说明

To fix, use ARRAY . 要修复,请使用ARRAY

For example, this query works: 例如,此查询有效:

 SELECT 1 x, (SELECT y FROM UNNEST(SPLIT("1")) y) y

在此处输入图片说明

But this one will give you the stated error: 但这会给您陈述的错误:

 SELECT 1 x, (SELECT y FROM UNNEST(SPLIT("1,2")) y) y

 "Scalar subquery produced more than one element"

And I can fix it with ARRAY() , that will produce nested repeated results: 我可以使用ARRAY()修复它,这将产生嵌套的重复结果:

 SELECT 1 x, ARRAY(SELECT y FROM UNNEST(SPLIT("1,2")) y) y

在此处输入图片说明

Or make sure to emit only one row, with LIMIT : 或确保只发出一行,并加上LIMIT

 SELECT 1 x, (SELECT y FROM UNNEST(SPLIT("1,2")) y LIMIT 1) y

在此处输入图片说明

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

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