子查询返回的值超过1。当子查询用作表达式时,不允许这样做。如何修复?

时间:2017-09-19 13:36:47

标签: mysql sql-server

我运行以下SQL:

SELECT 
    cusip, 
    symbol,
    CASE WHEN (U1 = 0 OR U2 = 0 OR U3 = 0) 
              THEN 0 END Universe_Flag,
    CASE WHEN (S1 = 'STOP' OR S2 = 'STOP' OR S3 = 'STOP') 
              THEN 'STOP' 
         WHEN (S1 = 'ACTIVE' OR S2 = 'ACTIVE' OR S3 = 'ACTIVE') 
              THEN 'ACTIVE' `enter code here`
    END Status

FROM (
    SELECT
        b.cusip,
        b.symbol,
        (SELECT UNIVERSE FROM sec_master m WHERE m.CUSIP = b.cusip) U1,
        (SELECT UNIVERSE FROM sec_master m WHERE m.SEDOL = b.cusip) U2,
        (SELECT UNIVERSE FROM sec_master m WHERE m.CINS  = b.cusip) U3,
        (SELECT SEC_STATUS FROM sec_master m WHERE m.CUSIP = b.cusip) S1,
        (SELECT SEC_STATUS FROM sec_master m WHERE m.SEDOL = b.cusip) S2,
        (SELECT SEC_STATUS FROM sec_master m WHERE m.CINS  = b.cusip) S3
    FROM (
        SELECT 
             e.cusip,
             e.symbol 
        FROM benchmark e 
        WHERE e.date > DATEADD(month, DATEDIFF(month,0,GETDATE()), 0)
    ) b

    LEFT JOIN sec_master x1 
        ON x1.CUSIP = b.cusip 
            AND x1.SEC_STATUS = 'ACTIVE' 
            AND x1.UNIVERSE = 1

    LEFT JOIN sec_master x2 
        ON x2.SEDOL = b.cusip 
            AND x2.SEC_STATUS = 'ACTIVE' 
            AND x2.UNIVERSE = 1

    LEFT JOIN sec_master x3 
        ON x3.CINS  = b.cusip 
            AND x3.SEC_STATUS = 'ACTIVE' 
            AND x3.UNIVERSE = 1

    WHERE x1.SEC_ID IS NULL 
        AND x2.SEC_ID IS NULL 
        AND x3.SEC_ID IS NULL
) X

但是返回以下错误:

  

Msg 512,Level 16,State 1,Line 69 Subquery返回超过1   值。当子查询跟随=!=<<=,&gt;时,不允许这样做。 >>=或子查询用作表达式时。

1 个答案:

答案 0 :(得分:0)

您的SELECT ... FROM sec_master m WHERE ...之一返回多行。您可以将TOP(1)ORDER BY子句一起使用,或将WHERE子句中的谓词更改为guarated只获得一行。