简体   繁体   中英

Assigning values to consecutive months

I have a table where I am looking at authorizations and their monthly value. I have taken the total value of the auth for the number of months and divided it by the number of months the auth is valid to give me a monthly amount. To aggregate the data, I need to somehow assign the monthly value to each month during that auth time frame. For example, I have an auth that started on 8/1/2017 and ends on 1/31/2018 which is 6 months in duration. The total value of the auth is $3559.50 so per month it is $593.25 so I need to somehow in my aggregate assign $593.25 to each month from August to January. I'm looking for this in results.

Month    member    amount
8/1/2017    12345   593.25
9/1/2017    12345   593.25
10/1/2017    12345   593.25
11/1/2017    12345   593.25
12/1/2017    12345   593.25
1/1/2018    12345   593.25

Here is sample data:

 create table #temp
 (month date,
 memberId varchar(5),
 auth_datefrom date,
 auth_dateto date,
 authmonths int,
 est_monthly_payment money)

INSERT INTO #temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','12345','8/1/2017','9/30/2017','2','762.75');
INSERT INTO #temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','67890','8/1/2017','9/30/2017','2','2440.8');
INSERT INTO #temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','23456','8/1/2017','6/30/2018','11','443.78');
INSERT INTO #temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','34567','8/1/2017','8/31/2017','1','1708.56');
INSERT INTO #temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','45678','8/1/2017','8/31/2017','1','4881.6');
INSERT INTO #temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','56789','8/1/2017','11/1/2017','3','996.66');
INSERT INTO #temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','67890','8/1/2017','6/30/2018','11','443.78');
INSERT INTO #temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','78901','8/1/2017','8/31/2017','1','1708.56');
INSERT INTO #temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','89012','8/1/2017','8/31/2017','1','4881.6');
INSERT INTO #temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','90123','8/1/2017','2/28/2018','7','813.6');
INSERT INTO #temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','1234','8/1/2017','6/30/2018','11','443.78');

It's a bit of a guess, but given the assumption that startdate is fixed, as in your sample code, this might work.

CREATE TABLE #DateTable (Dates DATETIME, ID INT IDENTITY )

DECLARE @Counter INT
SET @Counter = 0

WHILE @Counter < 481

BEGIN 
INSERT INTO #DateTable
SELECT DATEADD (MONTH, @Counter, '2017-08-01') 
SET @Counter = @Counter + 1

END

SELECT D.*, T.member_Id, T.auth_datefrom, T.auth_dateto, T.authmonths, 
T.est_monthly_payment 
FROM #DateTable AS D
CROSS JOIN  #temp AS T 
WHERE D.ID <= T.authmonths

So what I did was only convert it for a single user. You could turn this into a proc that would then give you the result set, and you can call the proc for each row of auths and add to a result set. If you want to do it in a single operation, you'll need to do some playing around with the CTE that gets you the dates.

In essence, the CTE loops over the date range for the row, and splits out each month, and then you just divide the total auth by duration between.

Here's how I did it.

declare @temp table
 ([month] date,
 member_id varchar(5),
 auth_datefrom date,
 auth_dateto date,
 authmonths int,
 est_monthly_payment money);

INSERT INTO @temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','12345','8/1/2017','9/30/2017','2','762.75');
INSERT INTO @temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','67890','8/1/2017','9/30/2017','2','2440.8');
INSERT INTO @temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','23456','8/1/2017','6/30/2018','11','443.78');
INSERT INTO @temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','34567','8/1/2017','8/31/2017','1','1708.56');
INSERT INTO @temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','45678','8/1/2017','8/31/2017','1','4881.6');
INSERT INTO @temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','56789','8/1/2017','11/1/2017','3','996.66');
INSERT INTO @temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','67890','8/1/2017','6/30/2018','11','443.78');
INSERT INTO @temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','78901','8/1/2017','8/31/2017','1','1708.56');
INSERT INTO @temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','89012','8/1/2017','8/31/2017','1','4881.6');
INSERT INTO @temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','90123','8/1/2017','2/28/2018','7','813.6');
INSERT INTO @temp (Month,member_id,auth_datefrom,auth_dateto,authmonths,Est_Monthly_Payment) VALUES ('8/1/2017','1234','8/1/2017','6/30/2018','11','443.78');

select * from @temp
WHERE
    member_id = '12345';

declare @startDate DateTime, @endDate DateTime;
select @startDate = auth_datefrom, @endDate = auth_dateto from @temp where member_id = '12345';

; with GetDates AS
(
    select dateadd(month, 1, @startDate) as TheDate
    UNION ALL
    select dateadd(month, 1, TheDate) from GetDates
    where TheDate < @endDate
)
select 
    t.member_id, gd.TheDate, (t.est_monthly_payment / t.authmonths) as monthly_payment

from 
    GetDates gd
    cross join @temp t

where
    t.member_id = '12345'
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM