为PaymentInterest生成日期日期

时间:2014-02-27 20:19:18

标签: sql sql-server

我需要为PaymentInterest生成grafic。

我有一个日期,我将生成从'31 / 01/2012'到'31 / 01/2015'的图形。下一个付款日期必须是'28 / 02/2012','31/03/2012'....直到'31 / 01/2015'是下个月的最后一天。 如果日期为'27 / 01/2012',则下一个日期必须为'27 / 02/2012','27/03/2012'....直到'31 / 01/2015'。

问题是,我有复选框“在周末之前”,如果我检查它们并且下一个日期是周末我需要在周末之前生成日期如果我取消选中“在周末之前”和下一个日期它是周末我需要生成周末之后的日期。

CCD.PercentDateFrom = '31/01/2012'

我有我的代码,但我不喜欢它们并且它不能正常工作

SELECT 
    PD.PercentRepaymentDate,
    PPP.MounthCount  --period 1 one month, 3 trimester, ..12 year and so on
FROM    CreditContracts CC
LEFT JOIN CreditContractDetails CCD 
    ON CCD.CreditContractID  = CC.id 
OUTER APPLY (
    SELECT
        CASE WHEN DAY(DATEADD(DAY,1,CCD.PercentDateFrom)) = 1 --verify if the next day is the first day of month is mean the previous date is last date of month
             THEN DATEADD(DAY,-1,DATEADD(MONTH, DATEDIFF(MONTH,0,DATEADD(MONTH, PPP.MounthCount * Number-1,CCD.PercentDateFrom))+1,0))
        ELSE DATEADD(MONTH,number + PPP.MounthCount - 1,ISNULL(CCD.PercentDateFrom, CCD.ContractSignDate)) 
        END [PercentRepaymentDate]
    FROM master.dbo.spt_values 
    WHERE Type = 'P'
        AND number <= CASE WHEN dbo.GetWorkDayByBranch(ISNULL(CCD.ContractExpireDate,dbo.GetMaxDateValue()),CC.BeforeWeekEnd,CCD.BranchID)<= '20120326' 
                            OR PPP.MounthCount = 0 
                            THEN 0 
                      ELSE DATEDIFF(MONTH,CCD.PercentDateFrom,CCD.ContractExpireDate)/PPP.MounthCount 
                      END
        ) PD    
WHERE CC.CreditContract=1

我的周末表是。

SELECT * FROM dbo.Weekends WHERE Date>='20120131'

Date    
2012-02-04 
2012-02-05
2012-02-11 
2012-02-12 
2012-02-18 
2012-02-19 
2012-02-25 
2012-02-26
2012-03-03 
2012-03-04 
2012-03-10 
2012-03-11 
2012-03-17 
2012-03-18 
2012-03-24 
2012-03-25 
2012-03-31 
2012-04-01 
2012-04-07 
2012-04-08 
2012-04-14 
2012-04-15 
.........

对不起我的作家,这是我的第一篇文章。

提前致谢!

1 个答案:

答案 0 :(得分:0)

我解决这个问题))

- GetWorkDayByBranch(Date smalldatetime,BeforeWeekend int)返回下一个或上一个工作日
           --IF BeforeWeekend = 1 SET BeforeWeekend = -1 ELSE SET BeforeWeekend = 1            --DECLARE @Result datetime            --SET @Result = @Date            --WHILE dbo.CheckWeekEndDayByBranch(@Result)= 1 - 从表中检查日期,如果存在,我们增加或减少日期取决于BeforeWeekend
           --BEGIN
           --SET @Result = DATEADD(day,BeforeWeekend,@ Result)             - 结束            --RETURN @Result

       --@Period Period can be 1-month ,3 - trimester ,6 -semester , 12 - year

       SELECT dbo.GetWorkDayByBranch(CASE dbo.fnIsEdnOfMonth(@DateFrom)
                                            WHEN 1 THEN CASE  
                                                            WHEN dbo.EndOfMonth(DATEADD(MONTH,@Period * number,@DateFrom)) > @DateTo
                                                            THEN @DateTo
                                                            ELSE dbo.EndOfMonth(DATEADD(MONTH,@Period * number,@DateFrom))
                                                        END 

                                            ELSE 
                                                        CASE  
                                                            WHEN DATEADD(MONTH,@Period * number,@DateFrom) > @DateTo
                                                            THEN @DateTo
                                                            ELSE DATEADD(MONTH,@Period*number , @DateFrom)
                                                        END
                                   END
                ,@BeforeWeekends,1) ,
                @BeforeWeekends
            FROM MASTER..spt_values 
            WHERE TYPE = 'P' AND number <= DATEDIFF(MONTH,@DateFrom,@DateTo) / @Period