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

时间:2019-03-13 17:29:08

标签: sql google-bigquery

根据BigQuery SQL编辑器,此查询“有效”。但是,当它运行时,会产生错误:标量子查询产生了多个元素

输入:

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

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

enter image description here

查询独立运行时的输出如下:

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

输出: enter image description here

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

输出: enter image description here

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

输出: enter image description here

3 个答案:

答案 0 :(得分:1)

标量子查询所产生的内容不能超过单行。您要显示的标量子查询显示单列多行。顾名思义,那是行不通的。

答案 1 :(得分:0)

我不使用子查询,而是使用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

输出: enter image description here

答案 2 :(得分:0)

要修复,请使用ARRAY

例如,此查询有效:

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

enter image description here

但是这个会给你陈述的错误:

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

 "Scalar subquery produced more than one element"

我可以用ARRAY()进行修复,这将产生嵌套的重复结果:

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

enter image description here

或者确保仅使用LIMIT发出一行:

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

enter image description here