简体   繁体   中英

Subquery returned more than 1 value. This is not permitted when the subquery follows =

I am trying to find 12 months of AR Amounts from a table. Grouping by a calculated date, which is on seperate table. this is the Query:

    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

I am getting the following error: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.1, Line 1 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.1, Line 1 .

Presumably, you want some sort of aggregation. Instead of:

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

Perhaps you want:

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

I think you want to use datepart, but Sample Data and desired output would help a lot.

AND datepart(year,datum) = 2019

instead of

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

In your sub query you are getting all the dates from 2nd table which are less than 2019, this is cause for the error. As mentioned if you are trying to get all the amounts for past 12 months from table 1 using dates from table2 you can try the following. - Write a join (left outer join between tab1 and tab2 on date col and in the where clause provide the date condition using between operator or using greater than or less than operator. Ex: 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'

If you do not prefer joins you can try with min/max operator.

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’) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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