SQL:填充缺少的月份和值

时间:2016-09-06 15:41:42

标签: sql missing-data

我的表格如下所示:

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之间缺少的月份和年份,其中新填充的条目的值将是先前填充的值 - 如果这样做会向后填充值任何意义?并且对于endyear / endmonth的条目,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

非常感谢任何帮助!

0 个答案:

没有答案