在SQL Server 2008中计算成熟度日期

时间:2017-10-16 10:08:55

标签: sql sql-server-2008

第一期付款日期是加入日期。客户必须支付10期分期付款。每个月的每一期。因此,11个月后,客户有资格购买节省金额的产品。因此到期日为加入日期后的11个月。 示例加入日期:2016年9月12日        到期日:2017年7月12日

如果客户在预定日期或该月到期日的任何一天支付,则不延长,否则延长到期日。

Scenario 1: Pays on time
------------------------

Joining date: 12/Sep/2016
Expected output: 12/Jul/2017
    Joining Date    12/Sep/2016 

Inst No  Payment Date   Actual Date payment Considered Month
1        12/Sep/2016    12/Sep/2016  Sep 2016
2        05/Oct/2016    12/Oct/2016  Oct 2016
3        21/Nov/2016    12/Nov/2016  Nov 2016
4        12/Dec/2016    12/Dec/2016  Dec 2016
5        02/Jan/2017    12/Jan/2017  Jan 2017
6        02/Feb/2017    12/Feb/2017  Feb 2017
7        06/Mar/2017    12/Mar/2017  Mar 2017
8        06/Apr/2017    12/Apr/2017  Apr 2017
9        01/May/2017    12/May/2017  May 2017
10       07/Jun/2017    12/Jun/2017  Jun 2017

        Maturity Date   12/Jul/2017

Scenario1

Scenario 2: when payments paid with delay
-----------------------------------------

Joining date: 12/Sep/2016
Expected output: 12/Sep/2017

Scenario2

   Joining Date    12/Sep/2016 

Inst No Payment Date    Actual Date Payment Considered Month
1       12/Sep/2016     12/Sep/2016  Sep 2016
2       05/Oct/2016     12/Oct/2016  Oct 2016
3       21/Nov/2016     12/Nov/2016  Nov 2016
4       02/Feb/2017     12/Dec/2016  Feb 2017
5       02/Feb/2017     12/Jan/2017  Mar 2017
6       06/Mar/2017     12/Feb/2017  Apr 2017
7       06/Mar/2017     12/Mar/2017  May 2017
8       01/Jun/2017     12/Apr/2017  Jun 2017
9       01/Jun/2017     12/May/2017  Jul 2017
10      07/Aug/2017     12/Jun/2017  Aug 2017

        Maturity Date   12/Sep/2017

3 个答案:

答案 0 :(得分:0)

您可以使用此公式。

string ageString = ageInt.ToString();

答案 1 :(得分:0)

试试这个 -

SELECT MATURITY_DATE = CASE WHEN MAX([ReceiptDate]) <= DATEADD (MM , 9, '2016-09-12 00:00:00')
                       THEN DATEADD (MM , 10, '2016-09-12 00:00:00')
                       ELSE DATEADD (MM , 2, MAX([ReceiptDate])) END
FROM [dbo].[Payment];

答案 2 :(得分:0)

您的问题不清楚您要实现的目标。但是,我从你的两个例子中得知的是,到期日应推迟支付分期付款延迟数月。

即使你的提琴手情景也不清楚。有了这个小小的理解,我想出了以下T-SQL来解决你的问题:

-- Schema generation
CREATE TABLE [dbo].[Payment](
    [InsNo] [int] PRIMARY KEY NOT NULL,
    [ReceiptDate] [datetime] NULL,
    [ScheduledDate] [datetime] NULL,
    [ConsideredMonth] DATETIME NULL,
) ON [PRIMARY]

GO

INSERT INTO [Payment]([InsNo], [ReceiptDate], [ScheduledDate]) 
    VALUES(1, '2016-09-12 00:00:00', '2016-09-12 00:00:00');

INSERT INTO [Payment]([InsNo], [ReceiptDate], [ScheduledDate]) 
    VALUES(2, '2016-10-05 00:00:00', '2016-10-12 00:00:00');

INSERT INTO [Payment]([InsNo], [ReceiptDate], [ScheduledDate]) 
    VALUES(3, '2016-11-21 00:00:00', '2016-11-12 00:00:00');

INSERT INTO [Payment]([InsNo], [ReceiptDate], [ScheduledDate]) 
    VALUES(4, '2017-02-02 00:00:00', '2016-12-12 00:00:00');

INSERT INTO [Payment]([InsNo], [ReceiptDate], [ScheduledDate]) 
    VALUES(5, '2017-02-02 00:00:00', '2016-01-12 00:00:00');

INSERT INTO [Payment]([InsNo], [ReceiptDate], [ScheduledDate]) 
    VALUES(6, '2017-02-02 00:00:00', '2016-02-12 00:00:00');

INSERT INTO [Payment]([InsNo], [ReceiptDate], [ScheduledDate]) 
    VALUES(7, '2017-03-06 00:00:00', '2016-03-12 00:00:00');

INSERT INTO [Payment]([InsNo], [ReceiptDate], [ScheduledDate]) 
    VALUES(8, '2017-03-06 00:00:00', '2016-04-12 00:00:00');

INSERT INTO [Payment]([InsNo], [ReceiptDate], [ScheduledDate]) 
    VALUES(9, '2017-06-01 00:00:00', '2016-05-12 00:00:00');

INSERT INTO [Payment]([InsNo], [ReceiptDate], [ScheduledDate]) 
    VALUES(10, '2017-06-01 00:00:00', '2016-06-12 00:00:00');

GO

--   Solution using Cursor          
DECLARE @receiptDate DATETIME
        ,@lastInstForDate DATETIME
DECLARE @insNo INT

DECLARE _paymentsCursor CURSOR FAST_FORWARD
FOR
    SELECT p.InsNo
            ,p.ReceiptDate
    FROM   Payment p
    ORDER BY
            p.InsNo

OPEN _paymentsCursor

FETCH NEXT FROM _paymentsCursor INTO
    @insNo, @receiptDate
WHILE @@FETCH_STATUS = 0
BEGIN
    SELECT @lastInstForDate = p.ConsideredMonth
    FROM   Payment p
    WHERE  p.InsNo = @insNo - 1

    IF DATEADD(MONTH ,1 ,@lastInstForDate) > @receiptDate
        UPDATE Payment
        SET ConsideredMonth      = DATEADD(MONTH ,1 ,@lastInstForDate)
        WHERE InsNo = @insNo
    ELSE
        UPDATE Payment
        SET    ConsideredMonth = CAST(CAST(YEAR(ReceiptDate) AS VARCHAR(4)) + RIGHT('0' + CAST(MONTH(ReceiptDate) AS VARCHAR(2)) ,2) + '01' AS DATETIME)
        WHERE  InsNo = @insNo

    FETCH NEXT FROM _paymentsCursor INTO
        @insNo, @receiptDate
END

CLOSE _paymentsCursor
DEALLOCATE _paymentsCursor


DECLARE @lastPaymentDate DATETIME

DECLARE @maturityDate DATETIME
SELECT @maturityDate = DATEADD(month, 1, MAX(p.ConsideredMonth)), @lastPaymentDate = MAX(p.ReceiptDate) FROM dbo.Payment p
SET @maturityDate = CAST(CAST(YEAR(@maturityDate) AS VARCHAR(4)) + RIGHT('0' + CAST(MONTH(@maturityDate) AS VARCHAR(2)) ,2) + RIGHT('0' + CAST(DAY(@lastPaymentDate) AS VARCHAR(2)) ,2) AS DATETIME)
SET @maturityDate = DATEADD(DAY, 1, @maturityDate)

SELECT @maturityDate