2

我有一个带有联合和派生表情况的存储过程。存储过程基本上需要返回所有Cases行 where Cases.AssnKey = AssnCtrl.pKey,包括不符合第一个表(因此是第二个)中的条件的行。

我已经尝试了几种方法。我的第一次尝试返回了所有符合条件的行,然后我了解到客户希望查看包含的所有案例。有一次,我有一个版本,它返回所有Case行加上符合第一个表中条件的行的重复项。在第二个表中,我试图从第一个表中排除记录以防止重复。

我已经到达以下,它解析,但抛出一个错误

“无效的对象名称'A'”。

我没有包括我的输入参数或声明,但它们都工作正常。任何帮助是极大的赞赏!

SELECT * 
FROM
   (SELECT 
       c.pKey, c.Name, c.LName, c.Balance, 
       SUM(cs.CAmount * @InterestRate * @DaysDiff) AS InterestAccrued, 
       ch.CollDesc, ac.Name AS AssociationName, (ac.IntrRate/100) AS IntrRate,
       SUM(CAmount) AS ChargeCodeBalance, c.MgmtKey,
       CASE c.PayPlanFlg
            WHEN 0 THEN 'No'
            WHEN 1 THEN 'Yes'
       END AS PayPlanFlg,
       CASE c.HoldFlg
            WHEN 0 THEN 'No'
            WHEN 1 THEN 'Yes'               
       END AS HoldFlg,
       CASE ac.AssmtChrgFreq
            WHEN 'D' THEN 'Daily'
            WHEN 'M' THEN 'Monthly'
            WHEN 'B' THEN 'Bi-monthly'
            WHEN 'Q' THEN 'Quarterly'
            WHEN 'A' THEN 'Annually'
            WHEN 'S' THEN 'Semi-annually'
            ELSE 'Unknown'
        END AS AssmtChrgFreq
    FROM Cases c 
    LEFT JOIN CaseSumm cs ON c.ClientKey = cs.ClientKey AND c.pKey = cs.CaseKey 
    INNER JOIN CollectHead ch ON c.ClientKey = ch.ClientKey AND ch.pKey = c.CollHeadKey 
    INNER JOIN AssnCtrl ac ON c.ClientKey = ac.ClientKey AND c.AssnKey = ac.pKey
    WHERE 
         c.ClientKey = @ClientKey
         AND c.AssnKey = @AssnKey   
         AND cs.TranCode IN (SELECT ChargeCode.RefNum.value('.', 'INT')
                             FROM @ChargeCodesXML.nodes('/chargecode/refnum') AS ChargeCode(RefNum))
        AND cs.TranDate >= @BeginDate 
        AND cs.TranDate <= @EndDate
        AND c.Active = 1
    GROUP BY c.pKey, c.Name, c.LName, c.Balance, ch.CollDesc, c.PayPlanFlg, c.HoldFlg, ac.Name, ac.IntrRate, ac.AssmtChrgFreq, c.MgmtKey) As A

    UNION       

    SELECT * FROM
    (SELECT c2.pKey, c2.Name, c2.LName, c2.Balance, 0 AS InterestAccrued, ch2.CollDesc, ac2.Name AS AssociationName, 0 AS IntrRate,
        0 AS ChargeCodeBalance, c2.MgmtKey,
        CASE c2.PayPlanFlg
            WHEN 0 THEN 'No'
            WHEN 1 THEN 'Yes'
        END
        AS PayPlanFlg,
        CASE c2.HoldFlg
            WHEN 0 THEN 'No'
            WHEN 1 THEN 'Yes'               
        END
        AS HoldFlg,
        CASE ac2.AssmtChrgFreq
            WHEN 'D' THEN 'Daily'
            WHEN 'M' THEN 'Monthly'
            WHEN 'B' THEN 'Bi-monthly'
            WHEN 'Q' THEN 'Quarterly'
            WHEN 'A' THEN 'Annually'
            WHEN 'S' THEN 'Semi-annually'
            ELSE 'Unknown'
        END
        AS AssmtChrgFreq
    FROM Cases c2 INNER JOIN CollectHead ch2
        ON c2.ClientKey = ch2.ClientKey
        AND ch2.pKey = c2.CollHeadKey INNER JOIN AssnCtrl ac2
        ON c2.ClientKey = ac2.ClientKey
        AND c2.AssnKey = ac2.pKey
    WHERE c2.ClientKey = @ClientKey
        AND c2.AssnKey = @AssnKey               
        AND c2.Active = 1
        AND c2.pKey NOT IN (SELECT pKey FROM A)
    GROUP BY c2.pKey, c2.Name, c2.LName, c2.Balance, ch2.CollDesc, c2.PayPlanFlg, c2.HoldFlg, ac2.Name, ac2.IntrRate, ac2.AssmtChrgFreq, c2.MgmtKey) As B           
4

1 回答 1

0

在您(select pKey from A)接近尾声时,您正在尝试使用 UNION 第一部分中子查询中的 A。您无法从那里访问它。这样做的方法是将 A 定义为 CTE,并访问它两次。

但是,请注意 A 会被评估两次,并且在极少数情况下,两者A's实际上可能会略有不同。

;WITH A AS (
    SELECT 
       c.pKey, c.Name, c.LName, c.Balance, 
       SUM(cs.CAmount * @InterestRate * @DaysDiff) AS InterestAccrued, 
       ch.CollDesc, ac.Name AS AssociationName, (ac.IntrRate/100) AS IntrRate,
       SUM(CAmount) AS ChargeCodeBalance, c.MgmtKey,
       CASE c.PayPlanFlg
            WHEN 0 THEN 'No'
            WHEN 1 THEN 'Yes'
       END AS PayPlanFlg,
       CASE c.HoldFlg
            WHEN 0 THEN 'No'
            WHEN 1 THEN 'Yes'               
       END AS HoldFlg,
       CASE ac.AssmtChrgFreq
            WHEN 'D' THEN 'Daily'
            WHEN 'M' THEN 'Monthly'
            WHEN 'B' THEN 'Bi-monthly'
            WHEN 'Q' THEN 'Quarterly'
            WHEN 'A' THEN 'Annually'
            WHEN 'S' THEN 'Semi-annually'
            ELSE 'Unknown'
        END AS AssmtChrgFreq
    FROM Cases c 
    LEFT JOIN CaseSumm cs ON c.ClientKey = cs.ClientKey AND c.pKey = cs.CaseKey 
    INNER JOIN CollectHead ch ON c.ClientKey = ch.ClientKey AND ch.pKey = c.CollHeadKey 
    INNER JOIN AssnCtrl ac ON c.ClientKey = ac.ClientKey AND c.AssnKey = ac.pKey
    WHERE 
         c.ClientKey = @ClientKey
         AND c.AssnKey = @AssnKey   
         AND cs.TranCode IN (SELECT ChargeCode.RefNum.value('.', 'INT')
                             FROM @ChargeCodesXML.nodes('/chargecode/refnum') AS ChargeCode(RefNum))
        AND cs.TranDate >= @BeginDate 
        AND cs.TranDate <= @EndDate
        AND c.Active = 1
    GROUP BY c.pKey, c.Name, c.LName, c.Balance, ch.CollDesc, c.PayPlanFlg, c.HoldFlg, ac.Name, ac.IntrRate, ac.AssmtChrgFreq, c.MgmtKey
)
SELECT * 
FROM A

    UNION       

    SELECT * FROM
    (SELECT c2.pKey, c2.Name, c2.LName, c2.Balance, 0 AS InterestAccrued, ch2.CollDesc, ac2.Name AS AssociationName, 0 AS IntrRate,
        0 AS ChargeCodeBalance, c2.MgmtKey,
        CASE c2.PayPlanFlg
            WHEN 0 THEN 'No'
            WHEN 1 THEN 'Yes'
        END
        AS PayPlanFlg,
        CASE c2.HoldFlg
            WHEN 0 THEN 'No'
            WHEN 1 THEN 'Yes'               
        END
        AS HoldFlg,
        CASE ac2.AssmtChrgFreq
            WHEN 'D' THEN 'Daily'
            WHEN 'M' THEN 'Monthly'
            WHEN 'B' THEN 'Bi-monthly'
            WHEN 'Q' THEN 'Quarterly'
            WHEN 'A' THEN 'Annually'
            WHEN 'S' THEN 'Semi-annually'
            ELSE 'Unknown'
        END
        AS AssmtChrgFreq
    FROM Cases c2 INNER JOIN CollectHead ch2
        ON c2.ClientKey = ch2.ClientKey
        AND ch2.pKey = c2.CollHeadKey INNER JOIN AssnCtrl ac2
        ON c2.ClientKey = ac2.ClientKey
        AND c2.AssnKey = ac2.pKey
    WHERE c2.ClientKey = @ClientKey
        AND c2.AssnKey = @AssnKey               
        AND c2.Active = 1
        AND c2.pKey NOT IN (SELECT pKey FROM A)
    GROUP BY c2.pKey, c2.Name, c2.LName, c2.Balance, ch2.CollDesc, c2.PayPlanFlg, c2.HoldFlg, ac2.Name, ac2.IntrRate, ac2.AssmtChrgFreq, c2.MgmtKey) As B
于 2012-10-02T21:22:11.190 回答