簡體   English   中英

為連續月份分配值

[英]Assigning values to consecutive months

我有一張桌子,在這里查看授權及其每月價值。 我已將auth的總值減去了幾個月的時間,然后將其除以auth有效的月數即可得出每月的金額。 為了匯總數據,我需要以某種方式將每月值分配給該身份驗證時間范圍內的每個月。 例如,我有一個身份驗證始於2017年8月1日,結束於1/31/2018,持續時間為6個月。 身份驗證的總價值為$ 3559.50,因此每月為$ 593.25,因此我需要以某種方式總計從8月到1月為每個月分配$ 593.25。 我正在尋找結果。

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

這是示例數據:

 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');

這有點猜測,但是假設您的示例代碼中的開始日期是固定的,那么這可能會起作用。

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

所以我所做的只是將其轉換為單個用戶。 您可以將其轉換為proc,然后為您提供結果集,並且可以針對auth的每一行調用proc並添加到結果集中。 如果要一次執行操作,則需要使用CTE來獲取日期,以便進行一些操作。

從本質上講,CTE在該行的日期范圍內循環,並每月​​進行拆分,然后您只需將總身份驗證除以持續時間即可。

這是我的方法。

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'
;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM