SQL:填充缺少的月份和值
我有一张如下所示的表:SQL:填充缺少的月份和值,sql,missing-data,Sql,Missing Data,我有一张如下所示的表: Accountno | MonthPeriod | YearPeriod | Value | StartYear | StartMonth | EndYear | Endmonth ---------- ------------- ------------ ------- ----------- ------------ --------- -------- 210 4 2015 100 2015
Accountno | MonthPeriod | YearPeriod | Value | StartYear | StartMonth | EndYear | Endmonth
---------- ------------- ------------ ------- ----------- ------------ --------- --------
210 4 2015 100 2015 1 2016 5
210 2 2016 200 2015 1 2016 5
300 4 2015 50 2015 1 2016 5
300 2 2016 100 2015 1 2016 5
我正在寻找一种方法来填充startmonth/startyear和endmonth/endyear之间缺少的月份和年份,其中新填充的条目的值将是以前填充的值-如果有意义,向后填充值?对于年末/月末的分录,accountno的最后一个值作为下表所需的输出
Accountno | MonthPeriod | YearPeriod | Value | StartYear | StartMonth | EndYear | Endmonth
---------- ------------- ------------ ------- ----------- ------------ --------- --------
210 1 2015 100 2015 1 2016 5
210 2 2015 100 2015 1 2016 5
210 3 2015 100 2015 1 2016 5
210 4 2015 200 2015 1 2016 5
210 5 2015 200 2015 1 2016 5
210 6 2015 200 2015 1 2016 5
210 7 2015 200 2015 1 2016 5
210 8 2015 200 2015 1 2016 5
210 9 2015 200 2015 1 2016 5
210 10 2015 200 2015 1 2016 5
210 11 2015 200 2015 1 2016 5
210 12 2015 200 2015 1 2016 5
210 1 2016 200 2015 1 2016 5
210 2 2016 200 2015 1 2016 5
210 3 2016 200 2015 1 2016 5
210 4 2016 200 2015 1 2016 5
210 5 2016 200 2015 1 2016 5
300 1 2015 50 2015 1 2016 5
300 2 2015 50 2015 1 2016 5
300 3 2015 50 2015 1 2016 5
300 4 2015 50 2015 1 2016 5
300 5 2015 100 2015 1 2016 5
300 6 2015 100 2015 1 2016 5
300 7 2015 100 2015 1 2016 5
300 8 2015 100 2015 1 2016 5
300 9 2015 100 2015 1 2016 5
300 10 2015 100 2015 1 2016 5
300 11 2015 100 2015 1 2016 5
300 12 2015 100 2015 1 2016 5
300 1 2016 100 2015 1 2016 5
300 2 2016 100 2015 1 2016 5
300 3 2016 100 2015 1 2016 5
300 4 2016 100 2015 1 2016 5
300 5 2016 100 2015 1 2016 5
我尝试针对我的情况操纵以下代码,但没有超过:
CREATE TABLE TEST( Month tinyint, Year int, Value int)
INSERT INTO TEST(Month, Year, Value)
VALUES
(1,2013,100),
(4,2013,101),
(8,2013,102),
(2,2014,103),
(4,2014,104)
DECLARE @Months Table(Month tinyint)
Insert into @Months(Month)Values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10), (11),(12);
With tblValues as (
select Rank() Over (ORDER BY y.Year, m.Month) as [Rank],
m.Month,
y.Year,
t.Value
from @Months m
CROSS JOIN ( Select Distinct Year from Test ) y
LEFT JOIN Test t on t.Month = m.Month and t.Year = y.Year
)
Select t.Month, t.Year, COALESCE(t.Value, t1.Value) as Value
from tblValues t
left join tblValues t1 on t1.Rank = (
Select Max(tmax.Rank)
From tblValues tmax
Where tmax.Rank < t.Rank AND tmax.Value is not null)
Order by t.Year, t.Month
任何帮助都将不胜感激 在现阶段,规则有点不透明,这使得很难提供帮助。你能解释一下月周期和年周期的含义吗?此外,如果同一帐户的不同行的endMonth和endYear不同会发生什么情况No?@wwkudu Monthperiod和Yearperiod是观察月份和观察年份,上面的示例中有一个条目,Accountno 210在2015年4月4日和2016年2月2日进行了一次观察——我想称之为ObservationMonth和ObservationYear更有意义?。所有Account的所有行的endMonth和endYear始终相同No的endMonth和endYear是一个“固定”值,我将输入该值,以计算StartMonth/StartYear和endMonth/endYear之间缺少的所有月份。这更有意义吗?在现阶段,规则有点不透明,因此很难提供帮助。你能解释一下月周期和年周期的含义吗?此外,如果同一帐户的不同行的endMonth和endYear不同会发生什么情况No?@wwkudu Monthperiod和Yearperiod是观察月份和观察年份,上面的示例中有一个条目,Accountno 210在2015年4月4日和2016年2月2日进行了一次观察——我想称之为ObservationMonth和ObservationYear更有意义?。所有Account的所有行的endMonth和endYear始终相同No的endMonth和endYear是一个“固定”值,我将输入该值,以计算StartMonth/StartYear和endMonth/endYear之间缺少的所有月份。这更有意义吗?