使用union和派生别名表从存储过程中的第一个表中排除结果

时间:2012-10-02 20:13:42

标签: sql union derived-table

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

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

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

  

“无效的对象名称'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           

1 个答案:

答案 0 :(得分: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