查找为SQL Server数据库中指定的最大日期指定的最大金额

时间:2012-07-25 15:47:55

标签: sql-server-2008 stored-procedures

好吧,所以我这个程序的目标是找到与付款承诺的最长日期相匹配的最高金额付款承诺。我将包括从中获取的过程,因此您将在此处看到2个过程。

这个第一个程序非常接近我想要它做的事,但是有两个问题。如果PaymentPromise表中有2个值具有相同的日期,并且该日期是最大值,则将返回这两个值。如果PaymentPromise中没有值但余额仍未结算,则应在PaymentPromise下返回null值,但不会这样做。

SQL Server 2008,您知道。

ALTER PROCEDURE dbo.GetPaymentsDue
(
@Username varchar(20)
)
AS
DECLARE @BondAmounts TABLE
(
BondID bigint,
BondAmount money,
BondTotal money,
BondBalance money
)

INSERT INTO @BondAmounts EXEC dbo.GetBondAmounts @Username = @Username

SELECT bonds.BondID, Bond.Agency, Bond.Agent, Bond.Defendant, Promise.ExpectedDate,
Promise.ExpectedAmount, bonds.BondTotal, bonds.BondBalance
FROM @BondAmounts bonds
INNER JOIN Bond ON bonds.BondID = Bond.ID
LEFT OUTER JOIN
(
    SELECT ID, BondID, ExpectedAmount, ExpectedDate, MAX(ExpectedDate) OVER (PARTITION BY BondID) MaxDate
    FROM PaymentPromise
) Promise ON bonds.BondID = Promise.BondID
WHERE bonds.BondBalance > 0
AND (Promise.ExpectedDate = Promise.MaxDate OR Promise.ExpectedDate = NULL)

因此,第一个过程调用第二个过程来获取初始数据集。

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
(
    SELECT Agency, Username FROM UserAgency
    WHERE Username = @Username
) AS 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)

1 个答案:

答案 0 :(得分:1)

您的查询很难理解。以下是在需要最大值时获取一行的一般策略:

with t as (select t.*,
                  row_number() over (partition by <whatever> order by paymentpromisedate desc) as seqnum
           from t
          )
select t.*
from t
where seqnum = 1

第二个可能是左外连接而不是连接的问题。

如果您可以缩小问题的范围 - 第一个或第二个查询 - 那么提供更详细的答案会更容易。

应用于您的查询。 。 。

SELECT bonds.BondID, Bond.Agency, Bond.Agent, Bond.Defendant, Promise.ExpectedDate,
       Promise.ExpectedAmount, bonds.BondTotal, bonds.BondBalance
FROM @BondAmounts bonds INNER JOIN
     Bond
     ON bonds.BondID = Bond.ID LEFT OUTER JOIN
     (SELECT ID, BondID, ExpectedAmount, ExpectedDate,
             row_number() over (partition by bondid order by expectedDate desc) as seqnum
      FROM PaymentPromise
     ) Promise
     ON bonds.BondID = Promise.BondID
WHERE bonds.BondBalance > 0 and
      (Promise.seqnum = 1 or Promise.seqnum is null)

我不确定为什么左连接不起作用。我正在思考它。