返回超过1个值的视图中的子查询

时间:2014-10-01 20:21:20

标签: sql sql-server tsql

我正在尝试创建一个视图,该视图具有执行计算的部分,以便决定要生成哪个字段。但是,我遇到了子查询的问题。

每当我尝试执行视图时,都会收到错误消息:Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

我理解它的含义,通常我会用WHERE子句和子查询中的参数来解决这个问题。但是,由于我需要将此作为一种观点,我该如何缓解这个问题?我已针对多个案例独立运行子查询,并验证它们确实每个订单返回一行。是否可以让子查询仅从视图中的当前行中选择单个结果?

以下是该视图特定部分的代码段。我们非常感谢您提出的任何建议。

SELECT
    CASE
        WHEN ORC.SimultaneousCalculation = 1
            THEN CASE
                    WHEN -- Select the Owner's Simultaneous Report Code when Sales Price >= Loan Amount.
                        (SELECT
                            CASE 
                                WHEN (ISNULL(O.OwnerPolicyLiability, 0) = 0)
                                    THEN O.SalesPrice
                                WHEN (ISNULL(O.OwnerPolicyLiability, 0) > 0)
                                    THEN (O.OwnerPolicyLiability)
                                ELSE 0 END AS 'SalesPrice'
                                FROM
                                    Orders O) >=
                        (SELECT
                            CASE 
                                WHEN (ISNULL(L.LoanPolicyLiability, 0) = 0)
                                    THEN L.Amount
                                WHEN (ISNULL(L.LoanPolicyLiability, 0) > 0)
                                    THEN (L.Amount)
                                ELSE 0 END AS 'LoanAmount'
                                FROM
                                    Orders O
                                    INNER JOIN Loan L
                                        ON O.OrdersID = L.OrdersID
                                        AND L.LoanOrder = 1)
                        THEN CONVERT(VARCHAR(MAX),RES.SalesPolicyCode)
                    WHEN -- Select the Owner's Simultaneous Report Code when Sales Price < Loan Amount.
                        (SELECT
                            CASE 
                                WHEN (ISNULL(O.OwnerPolicyLiability, 0) = 0)
                                    THEN O.SalesPrice
                                WHEN (ISNULL(O.OwnerPolicyLiability, 0) > 0)
                                    THEN (O.OwnerPolicyLiability)
                                ELSE 0 END AS 'SalesPrice'
                                FROM
                                    Orders O) <
                        (SELECT
                            CASE 
                                WHEN (ISNULL(L.LoanPolicyLiability, 0) = 0)
                                    THEN L.Amount
                                WHEN (ISNULL(L.LoanPolicyLiability, 0) > 0)
                                    THEN (L.Amount)
                                ELSE 0 END AS 'LoanAmount'
                                FROM
                                    Orders O
                                    INNER JOIN Loan L
                                        ON O.OrdersID = L.OrdersID
                                        AND L.LoanOrder = 1)
                        THEN CONVERT(VARCHAR(MAX),RES.LoanPolicyCode)
                    ELSE CONVERT(VARCHAR(MAX),0) END
        -- If not Simultaneous, select the primary rate's Report Code.
        ELSE CONVERT(VARCHAR(MAX),R.ReportCode)
        END AS 'PremCode'
    ,O.Col1
    ,O.Col2
    ,O.Col3
FROM
    Orders O
    INNER JOIN OrderRateCalculation ORC
        ON O.OrdersID = ORC.OrdersID
    LEFT JOIN Rate R
        ON ORC.RateID = R.RateID
    LEFT JOIN RateEffectiveDate RED
        ON R.RateID = RED.RateID
    LEFT JOIN RateEngineSimultaneous RES
        ON RED.RateEffectiveDateID = RES.RateEffectiveDateID
WHERE
    (ORC.LoanHolder = 1)

1 个答案:

答案 0 :(得分:1)

如果不了解您的牌桌,这很难回答。关系,但如果是我,我会试着找到一种方法将JOIN转移到&#39; Loan&#39;到主要连接列表,并完全删除子选择。像这样:

SELECT
CASE
    WHEN ORC.SimultaneousCalculation = 1
        THEN CASE
                WHEN -- Select the Owner's Simultaneous Report Code when Sales Price >= Loan Amount.
                        CASE 
                            WHEN (ISNULL(O.OwnerPolicyLiability, 0) = 0)
                                THEN O.SalesPrice
                            WHEN (ISNULL(O.OwnerPolicyLiability, 0) > 0)
                                THEN (O.OwnerPolicyLiability)
                            ELSE 0 END AS 'SalesPrice'
                         >=
                        CASE 
                            WHEN (ISNULL(L.LoanPolicyLiability, 0) = 0)
                                THEN L.Amount
                            WHEN (ISNULL(L.LoanPolicyLiability, 0) > 0)
                                THEN (L.Amount)
                            ELSE 0 END AS 'LoanAmount'
                    THEN CONVERT(VARCHAR(MAX),RES.SalesPolicyCode)
                WHEN -- Select the Owner's Simultaneous Report Code when Sales Price < Loan Amount.
                        CASE 
                            WHEN (ISNULL(O.OwnerPolicyLiability, 0) = 0)
                                THEN O.SalesPrice
                            WHEN (ISNULL(O.OwnerPolicyLiability, 0) > 0)
                                THEN (O.OwnerPolicyLiability)
                            ELSE 0 END AS 'SalesPrice'
                         <
                        CASE 
                            WHEN (ISNULL(L.LoanPolicyLiability, 0) = 0)
                                THEN L.Amount
                            WHEN (ISNULL(L.LoanPolicyLiability, 0) > 0)
                                THEN (L.Amount)
                            ELSE 0 END AS 'LoanAmount'
                    THEN CONVERT(VARCHAR(MAX),RES.LoanPolicyCode)
                ELSE CONVERT(VARCHAR(MAX),0) END
    -- If not Simultaneous, select the primary rate's Report Code.
    ELSE CONVERT(VARCHAR(MAX),R.ReportCode)
    END AS 'PremCode'
,O.Col1
,O.Col2
,O.Col3
FROM
Orders O
INNER JOIN OrderRateCalculation ORC
    ON O.OrdersID = ORC.OrdersID
LEFT JOIN Rate R
    ON ORC.RateID = R.RateID
LEFT JOIN RateEffectiveDate RED
    ON R.RateID = RED.RateID
LEFT JOIN RateEngineSimultaneous RES
    ON RED.RateEffectiveDateID = RES.RateEffectiveDateID
LEFT JOIN Loan L
    ON O.OrdersID = L.OrdersID
    AND L.LoanOrder = 1
WHERE
(ORC.LoanHolder = 1)

祝你好运!