【问题标题】:Subquery returned more than 1 value. This is not permitted when the subquery follows =子查询返回超过 1 个值。当子查询遵循 = 时,这是不允许的
【发布时间】:2019-01-14 18:05:38
【问题描述】:

我正在尝试从表格中查找 12 个月的 AR 金额。按单独表格上的计算日期分组。这是查询:

    SELECT ROUND(SUM(bdr_hfl),2) AS AmountDC , datum
    FROM gbkmut with (NOLOCK)
    WHERE reknr in (1300,1320) 
        AND kstdrcode BETWEEN '00' AND '10'
        AND kstplcode = '00'
        AND transtype IN ('N', 'C', 'P') 
        AND ISNULL(transsubtype, '') <> 'X'
        AND datum <= (SELECT eddatum FROM perdat 
                        WHERE bkjrcode = 2019
                    GROUP BY EDDATUM)
    GROUP BY DATUM, BDR_HFL

我收到以下错误: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, &lt;, &lt;= , &gt;, &gt;= or when the subquery is used as an expression.1, Line 1.

【问题讨论】:

    标签: sql subquery


    【解决方案1】:

    大概,您想要某种聚合。而不是:

    datum <= (SELECT eddatum FROM perdat WHERE bkjrcode = 2019 GROUP BY EDDATUM) 
    

    也许你想要:

    datum <= (SELECT MIN(p.eddatum)
              FROM perdat p
              WHERE bkjrcode = 2019
             ) 
    

    【讨论】:

    • 您好,我想查看所有 12 个日期。所以我可以把它按整整 12 个月分组..
    【解决方案2】:

    我认为您想使用 datepart,但示例数据和所需的输出会有很大帮助。

    AND datepart(year,datum) = 2019
    

    而不是

    AND datum <= (SELECT eddatum FROM perdat 
                    WHERE bkjrcode = 2019
                GROUP BY EDDATUM)
    

    【讨论】:

      【解决方案3】:

      在您的子查询中,您从第二个表中获取小于 2019 年的所有日期,这是导致错误的原因。 如前所述,如果您尝试使用表 2 中的日期从表 1 中获取过去 12 个月的所有金额,您可以尝试以下操作。 - 写一个连接(tab1 和 tab2 之间的左外连接在日期 col 和 where 子句中使用 between 运算符或使用大于或小于运算符提供日期条件。 例如:select round(a.col1,2),a.col2 from tab1 a inner join/ left outer join tab2 bon a.datecol=b.datecol Where a.cond1=xxxx and a.cond2=yyyy.....etc a.datecol>= ‘01-01-2018’ and a.datecol <=‘31-12-2018’

      如果您不喜欢连接,可以尝试使用 min/max 运算符。

      Select col1,col2 From tab1 where datecol>=(select min(datecol) from tab2 where yearpart(datecol)<‘2019’) and datecol>= select max(datecol) from tab2 where yearpart(datecol)<‘2019’) 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-04
        • 2016-08-02
        • 2019-10-13
        • 1970-01-01
        相关资源
        最近更新 更多