简体   繁体   中英

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, , >= or when the subquery is used as an expr

SELECT
    INT_ID, FST_NAME, LAST_NAME, MID_NAME, FIRST_NAME,LAST_NAME, MIDDLE_NAME, Null, Null,
    CASE WHEN LEVELS = 'Done' THEN 1 ELSE NULL END AS Verified,
    NULL
FROM Customer_Table
WHERE 
    CREATEDDATE = (SELECT DISTINCT MAX(CREATEDDATE) FROM Customer_Table  GROUP BY INT_ID)

I got the following error message from sql server.

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Obviously, this subquery can return more than one value:

where CREATEDDATE = (Select Distinct max(CREATEDDATE) From Customer_Table  group by INT_ID )

I am pretty sure you intend a correlated subquery instead:

select . . .
from Customer_Table ct
where ct.createddate = (select max(ct2.createddate)
                        from Customer_Table ct2
                        where ct2.int_id = ct.int_id
                       );

This sounds like you want a max per group query:

SELECT
    c1.INT_ID,
    c1.FST_NAME,
    c1.LAST_NAME,
    c1.MID_NAME,
    c1.FIRST_NAME,
    c1.LAST_NAME,
    c1.MIDDLE_NAME,
    NULL,
    NULL,
    CASE WHEN c1.LEVELS = 'Done' THEN 1 END AS Verified,
    NULL
FROM Customer_Table c1
INNER JOIN
(
    SELECT INT_ID, MAX(CREATEDDATE) AS max_date
    FROM Customer_Table
    GROUP BY INT_ID
) c2
    ON c1.INT_ID = c2.INT_ID AND
       c1.CREATEDDATE = c2.max_date;

There is a nicer (and possibly more performant) way of doing this using ROW_NUMBER , but your version of MySQL may not support it.

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