我有一张桌子,我正在查看授权及其每月价值。我已经将auth的总值取了几个月,然后除以auth有效的月数给我每月的金额。为了聚合数据,我需要以某种方式在该身份验证时间框架内为每个月分配月度值。例如,我有一个auth,开始于2017年8月1日,结束于1月31日,持续6个月。 auth的总价值为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');
答案 0 :(得分:0)
这是一个猜测,但假设startdate是固定的,就像在示例代码中一样,这可能会有效。
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
答案 1 :(得分:0)
所以我所做的只是将其转换为单个用户。您可以将其转换为proc,然后为您提供结果集,并且可以为每行auths调用proc并添加到结果集。如果你想在一次操作中完成它,你需要做一些可以获得日期的CTE。
本质上,CTE循环遍历行的日期范围,并在每个月拆分,然后您只需将总auth除以之间的持续时间。
我是这样做的。
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'
;