1

I have a payment_schedule table:

CREATE TABLE [dbo].[scheduled_transaction](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [description] [varchar](20) NOT NULL,
    [account_id] [int] NOT NULL,
    [account_transaction_type_id] [int] NOT NULL,
    [third_party_id] [int] NOT NULL,
    [first_payment_date] [date] NOT NULL,
    [last_payment_date] [date] NULL,
    [payment_frequency] [int] NOT NULL,
    [payment_frequency_type_id] [int] NOT NULL,
    [payment_amount] [decimal](18, 2) NOT NULL,
    [notes] [varchar](100) NULL,
    [deleted] [datetime] NULL,
    [createuser] [int] NOT NULL,
    [createdate] [datetime] NOT NULL,
    [lastupdateuser] [int] NULL,
    [lastupdatedate] [datetime] NULL) ON [PRIMARY]
)

This table holds scheduled bill payments for my home system. The frequency is simply Daily, weekly or monthly. So, payment frequency = 1, and payment frequency type = 3 (monthly) means that the payment is done every one month.

I also have a calendar table, which is a table of all dates between a large period (2000 and 2040). This is just a reference table that I think is useful for what I will be doing.

What I want to do now, is create a procedure that will return a table of dates from a given startdate to a given enddate, and for each date, return any payments that should be done on that date, based on my schedule table.

My plan is to create a temp table with all dates where payments will be due:

DECLARE @StartDate DATE
DECLARE @EndDate DATE

Set @StartDate = '01-JAN-2013'
SET @EndDate = '31-DEC-2013'

DECLARE @Schedule TABLE
(
    ID INT NOT NULL IDENTITY(1,1),
    TransactionDate DATE NOT NULL,
    scheduled_transaction_id INT NOT NULL

)

Once that's populated, I can then use the calendar table and create a balance forecast.

But, getting the data into that table is tricky.

I think I need to go through each scheduled_transaction, and then, run through the calendar and see if a transaction would be done on that date? And then insert the row into my temp table?

So, then I think it would be nested cursors. For each scheduled_transaction row, then for each calendar row, and use some form of 'DATEADD' or something?

Could anyone assist me with this?

4

2 回答 2

0

我假设您有 #days 表,其中包含日历中每一天的日期,因此 2013-01-01、2013-01-02 等。

另外我不知道你什么时候付钱给某人,每月的时间表和第一次付款日期为 2013-01-31...?所以我会忽略这种情况:

select d.d as [day], st.id as transactionId
from #days d
inner join [scheduled_transaction] st
on (d.d >= st.first_payment_date 
    and d.d <= st.last_payment_date
    and (
            (
            st.payment_frequency = 2
            and datediff(day, d.d, st.first_payment_date) % 7 = 0
            )
            or
            (
            st.payment_frequency = 1
            )
            or
            (st.payment_frequency = 3
            and day(st.first_payment_date) = day(d.d)
            )
        )
    )
于 2013-10-15T12:14:17.650 回答
0

这可以使用常见的 Table 表达式来完成。执行以下代码以列出给定日期范围内的所有到期付款。

DECLARE @StartDate DATE
DECLARE @EndDate DATE

Set @StartDate = '01-JAN-2013'
SET @EndDate = '31-DEC-2013'

;with schedule as
    (
        SELECT first_payment_date as Next_payment_date,* FROM scheduled_transaction 
        UNION ALL
        SELECT  DATEADD( dd,case when payment_frequency_type_id =1 then 1  -- Daily
                            else case when payment_frequency_type_id =2 then 7  --Weekly
                            else 30 end end --Monthly
                        ,Next_payment_date) ,  
                id, description, account_id, account_transaction_type_id, third_party_id, first_payment_date, last_payment_date, payment_frequency, payment_frequency_type_id, 
                payment_amount, notes, deleted, createuser, createdate, lastupdateuser, lastupdatedate
        FROM        schedule
        WHERE Next_payment_date < last_payment_date
    )
    SELECT  * FROM SCHEDULE 
WHERE Next_payment_date between @StarDate and @EndDate

此代码假定付款频率存储在 payment_frequency_type_id 中。如果不同,请相应地更改代码。

于 2013-10-17T05:13:02.543 回答