1

我在 SQL Server 2005 中有以下存储过程:

ALTER PROCEDURE dbo.GetBondAmounts
    (
    @Username varchar(20)
    )
AS
    SELECT Bond.ID BondID, Powers.BondAmount + Charges.BondAmount BondAmount,
    BondFee.Amount + Powers.BondPremium + Charges.BondPremium + Forfeiture.CostOfApprehension + Forfeiture.AmountPaid BondTotal,
    BondFee.Amount + Powers.BondPremium + Charges.BondPremium + Forfeiture.CostOfApprehension + Forfeiture.AmountPaid
    - BalanceForgiveness.Amount - Payment.Amount BondBalance
    FROM Bond LEFT OUTER JOIN UserAgency ON Bond.Agency = UserAgency.Agency
    LEFT OUTER JOIN
    (
        SELECT BondID, SUM(AmountForgiven) Amount
        FROM BalanceForgiveness
        GROUP BY BondID
    ) AS BalanceForgiveness ON Bond.ID = BalanceForgiveness.BondID
    LEFT OUTER JOIN
    (
        SELECT Bond, SUM(Amount) Amount
        FROM BondFee
        GROUP BY Bond
    ) AS BondFee ON Bond.ID = BondFee.Bond
    LEFT OUTER JOIN
    (
        SELECT Powers.Bond, SUM(Charge.BondAmount) BondAmount,
        SUM(Charge.BondPremium) BondPremium
        FROM Powers INNER JOIN Charge ON Powers.Surety = Charge.PowerSurety
        AND Powers.PowerPrefix = Charge.PowerPrefix AND Powers.PowerNumber = Charge.PowerNumber
        GROUP BY Bond
    ) AS Powers ON Bond.ID = Powers.Bond
    LEFT OUTER JOIN
    (
        SELECT BondID, SUM(BondAmount) BondAmount, SUM(BondPremium) BondPremium
        FROM ChargeWithoutPower
        GROUP BY BondID
    ) AS Charges ON Bond.ID = Charges.BondID
    LEFT OUTER JOIN
    (
        SELECT Bond, SUM(CostOfApprehension) CostOfApprehension, SUM(AmountPaid) AmountPaid
        FROM Forfeiture
        GROUP BY Bond
    ) AS Forfeiture ON Bond.ID = Forfeiture.Bond
    LEFT OUTER JOIN
    (
        SELECT Bond, SUM(Amount) Amount
        FROM Payment
        GROUP BY Bond
    ) AS Payment ON Bond.ID = Payment.Bond
    WHERE UserAgency.Username = @Username
    OR EXISTS (SELECT * FROM Users WHERE Username = @Username AND Admin = 1)

我得到了 BondAmount、BondTotal 和 BondBalance 的 dbNull 值。有时不会有 Charge 行或 BalanceForgiveness 行等。我认为我遇到的问题是,当有一个表不包含该行的数据时,整个计算变为空......我该如何解决这个问题给我每个债券的正确金额、总额和余额,无论有多少表有数据或没有数据。

4

2 回答 2

5

把你的总和包起来ISNULL

ISNULL (SUM(Charge.BondAmount), 0)

如果您希望在没有要求和的元素时总和为 0。

于 2012-05-16T20:19:10.400 回答
0

@Albin:感谢您帮助我找到此解决方案。在 ISNULL 中包装 SUM 实际上什么也没做,但是当我执行以下操作时,它起作用了。感谢您帮助我找到解决方案。

ALTER PROCEDURE dbo.GetBondAmounts
    (
    @Username varchar(20)
    )
AS
    SELECT Bond.ID BondID, (ISNULL(Powers.Amount,0) + ISNULL(Charges.Amount,0)) BondAmount,
    (ISNULL(BondFee.Amount,0) + ISNULL(Powers.Premium,0) + ISNULL(Charges.Premium,0)
    + ISNULL(Forfeiture.CostOfApprehension,0) + ISNULL(Forfeiture.AmountPaid,0) + Bond.StateTax) BondTotal,
    (ISNULL(BondFee.Amount,0) + ISNULL(Powers.Premium,0) + ISNULL(Charges.Premium,0)
    + ISNULL(Forfeiture.CostOfApprehension,0) + ISNULL(Forfeiture.AmountPaid,0) + Bond.StateTax
    - ISNULL(BalanceForgiveness.Amount,0) - ISNULL(Payment.Amount,0)) BondBalance
    FROM Bond LEFT OUTER JOIN UserAgency ON Bond.Agency = UserAgency.Agency
    LEFT OUTER JOIN
    (
        SELECT BondID, SUM(AmountForgiven) Amount
        FROM BalanceForgiveness
        GROUP BY BondID
    ) AS BalanceForgiveness ON Bond.ID = BalanceForgiveness.BondID
    LEFT OUTER JOIN
    (
        SELECT Bond, SUM(Amount) Amount
        FROM BondFee
        GROUP BY Bond
    ) AS BondFee ON Bond.ID = BondFee.Bond
    LEFT OUTER JOIN
    (
        SELECT Powers.Bond, SUM(Charge.BondAmount) Amount,
        ISNULL(SUM(Charge.BondPremium), 0) Premium
        FROM Powers INNER JOIN Charge ON Powers.Surety = Charge.PowerSurety
        AND Powers.PowerPrefix = Charge.PowerPrefix AND Powers.PowerNumber = Charge.PowerNumber
        GROUP BY Bond
    ) AS Powers ON Bond.ID = Powers.Bond
    LEFT OUTER JOIN
    (
        SELECT BondID, SUM(BondAmount) Amount, SUM(BondPremium) Premium
        FROM ChargeWithoutPower
        GROUP BY BondID
    ) AS Charges ON Bond.ID = Charges.BondID
    LEFT OUTER JOIN
    (
        SELECT Bond, SUM(CostOfApprehension) CostOfApprehension, SUM(AmountPaid) AmountPaid
        FROM Forfeiture
        GROUP BY Bond
    ) AS Forfeiture ON Bond.ID = Forfeiture.Bond
    LEFT OUTER JOIN
    (
        SELECT Bond, SUM(Amount) Amount
        FROM Payment
        GROUP BY Bond
    ) AS Payment ON Bond.ID = Payment.Bond
    WHERE UserAgency.Username = @Username
    OR EXISTS (SELECT * FROM Users WHERE Username = @Username AND Admin = 1)
于 2012-05-17T14:38:56.500 回答