0

我设置了一张表格,这是客户对产品的还款计划。

我的表由以下字段组成:

销售ID | 付款到期日 | 应付金额

每次销售我需要的是:

销售ID | 首次付款日期 | FirstPaymentValue | 定期付款值 | 最终付款价值

每个还款计划都有以下特点:

首付款金额由客户定义。定期和最终付款由余额决定。

例如

销售 = £200 期限 = 14 周(14 次付款)

首付款 = 50 英镑 (x1)

定期付款 = 12 英镑 (x12)

最终付款 = 6 英镑 (x1)

请你能告诉我如何检索我需要的东西。

补充:到目前为止的代码:

这是我到目前为止所取得的成就,但这只会让我获得每次销售的首次付款和首次付款日期:

SELECT      
FP.SaleID, FP.FirstPaymentDate, RS.AmountDue AS FirstPayment 
FROM    
(       
SELECT SaleID, MIN(PaymentDueDate)AS FirstPaymentDate       
FROM RepaymentSchedule          
GROUP BY SaleID     
) AS FP 
LEFT OUTER JOIN
RepaymentSchedule AS RS ON FP.SaleID = RS.SaleID AND FP.FirstPaymentDate =   RS.PaymentDueDate ORDER BY RS.SaleID

附加:数据样本:14 周内 280 英镑

SaleID  PaymentDueDate            AmountDue
41      2012-08-29 00:00:00.000   120.00
41      2012-09-05 00:00:00.000   12.30
41      2012-09-12 00:00:00.000   12.30
41      2012-09-19 00:00:00.000   12.30
41      2012-09-26 00:00:00.000   12.30
41      2012-10-03 00:00:00.000   12.30
41      2012-10-10 00:00:00.000   12.30
41      2012-10-17 00:00:00.000   12.30
41      2012-10-24 00:00:00.000   12.30
41      2012-10-31 00:00:00.000   12.30
41      2012-11-07 00:00:00.000   12.30
41      2012-11-14 00:00:00.000   12.30
41      2012-11-21 00:00:00.000   12.30
41      2012-11-28 00:00:00.000   12.40

我猜 FinalPaymentValue 并不太重要。

真的,我只需要知道如何计算出定期付款是多少。

4

1 回答 1

1

这应该有效:

SELECT pd.saleid,
       pd.firstpaymentdate                               AS FirstPaymentDate,
       (SELECT amountdue
        FROM   repaymentschedule
        WHERE  saleid = pd.saleid
               AND paymentduedate = pd.firstpaymentdate) AS FirstPaymentValue,
       (SELECT TOP 1 amountdue
        FROM   repaymentschedule
        WHERE  saleid = pd.saleid
               AND paymentduedate <> pd.firstpaymentdate
               AND paymentduedate <> pd.lastpaymentdate) AS RegularPaymentValue,
       (SELECT amountdue
        FROM   repaymentschedule
        WHERE  saleid = pd.saleid
               AND paymentduedate = pd.lastpaymentdate)  AS FinalPaymentValue
FROM   (SELECT saleid,
               Min(paymentduedate) AS FirstPaymentDate,
               Max(paymentduedate) AS LastPaymentDate
        FROM   repaymentschedule
        GROUP  BY saleid) pd 

看到它在行动

样本结果:

| 销售标识 | 第一次付款日期 | 首付款价值 | 常规支付价值 | FINALPAYMENTVALUE |
-------------------------------------------------- -------------------------------------------------- ----
| 41 | 2012 年 8 月 29 日 00:00:00+0000 | 120 | 12.3 | 12.4 |
于 2012-10-31T17:18:01.683 回答