2014-09-19 54 views
1

我的表在我需要從前面的行中填寫的列中有某些空白值。我的源數據目前看起來像來自SQL Server中先前記錄的FIll空值

Row ID     YEAR Period NUMBER PeriodYear 
49 000000000130000000 2014 4  NULL 4/1/2014 
50 000000000130000000 2014 3  286.26 3/1/2014 
51 000000000130000000 2014 2  NULL 2/1/2014 
52 000000000130000000 2014 1  NULL 1/1/2014 
53 000000000130000000 2013 12  286.26 12/1/2013 
54 000000000130000000 2013 11  NULL 11/1/2013 
55 000000000130000000 2013 10  NULL 10/1/2013 
56 000000000130000000 2013 9  286.26 9/1/2013 
57 000000000130000000 2013 8  NULL 8/1/2013 
58 000000000130000000 2013 7  NULL 7/1/2013 
59 000000000130000000 2013 6  286.26 6/1/2013 
60 000000000130000000 2013 5  NULL 5/1/2013 
61 000000000130000000 2013 4  286.26 4/1/2013 
62 000000000130000000 2013 3  291.98 3/1/2013 
63 000000000130000000 2013 2  NULL 2/1/2013 
64 000000000130000000 2013 1  291.98 1/1/2013 
65 000000000130000000 2012 12  280.49 12/1/2012 
66 000000000130000000 2012 11  280.49 11/1/2012 
67 000000000130000000 2012 10  280.49 10/1/2012 
68 000000000130000000 2012 9  289.96 9/1/2012 
69 000000000130000000 2012 8  NULL 8/1/2012 
70 000000000130000000 2012 7  289.96 7/1/2012 
71 000000000130000000 2012 6  294.54 6/1/2012 
72 000000000130000000 2012 5  NULL 5/1/2012 

我希望目標的樣子:

Row ID     YEAR Period NUMBER PeriodYear 
49 000000000130000000 2014 4  NULL 4/1/2014 
50 000000000130000000 2014 3  286.26 3/1/2014 
51 000000000130000000 2014 2  286.26 2/1/2014 
52 000000000130000000 2014 1  286.26 1/1/2014 
53 000000000130000000 2013 12  286.26 12/1/2013 
54 000000000130000000 2013 11  286.26 11/1/2013 
55 000000000130000000 2013 10  286.26 10/1/2013 
56 000000000130000000 2013 9  286.26 9/1/2013 
57 000000000130000000 2013 8  286.26 8/1/2013 
58 000000000130000000 2013 7  286.26 7/1/2013 
59 000000000130000000 2013 6  286.26 6/1/2013 
60 000000000130000000 2013 5  286.26 5/1/2013 
61 000000000130000000 2013 4  286.26 4/1/2013 
62 000000000130000000 2013 3  291.98 3/1/2013 
63 000000000130000000 2013 2  291.98 2/1/2013 
64 000000000130000000 2013 1  291.98 1/1/2013 
65 000000000130000000 2012 12  280.49 12/1/2012 
66 000000000130000000 2012 11  280.49 11/1/2012 
67 000000000130000000 2012 10  280.49 10/1/2012 
68 000000000130000000 2012 9  289.96 9/1/2012 
69 000000000130000000 2012 8  289.96 8/1/2012 
70 000000000130000000 2012 7  289.96 7/1/2012 
71 000000000130000000 2012 6  294.54 6/1/2012 
72 000000000130000000 2012 5  294.54 5/1/2012 

任何幫助將不勝感激。

+3

哪個版本?如果是2012年,你可能會使用滯後。 – 2014-09-19 08:41:01

+0

@GiannisParaskevopoulos:做'鉛'和'滯後'也有條件工作?所以就像「給我第一個'PeriodYear'與'ID'不是null並且有比我更低的'Row''。 – 2014-09-19 08:53:49

回答

1

使用子查詢:

UPDATE t 
SET t.NUMBER = (SELECT TOP 1 t2.PeriodYear 
       FROM dbo.TableName t2 
       WHERE t2.ID = t.ID 
        AND t2.Row < t.Row 
        AND t2.NUMBER IS NOT NULL) 
FROM dbo.TableName t 
WHERE t.NUMBER IS NULL 
+0

要更新的列是'數字'而不是'PeriodYear' – 2014-09-19 09:01:18

+0

@wewesthemenace:謝謝,現在很清楚,因爲它已被格式化。 – 2014-09-19 09:05:04

0

這裏有一個方法:

SELECT 1 

WHILE @@ROWCOUNT <> 0 
BEGIN 
    UPDATE Table 
    SET [NUMBER] = Prior.[NUMBER] 
    FROM Table Current 
    INNER JOIN Table Prior 
    ON Prior.[Row] = Current.[Row] -1 
    AND Current.[NUMBER] IS NULL 
    AND Prior.[NUMBER] IS NOT NULL 
END 
+0

@@ ROWCOUNT不需要ORDER BY嗎? – Mihai 2014-09-19 09:31:05

+0

我不知道我明白。爲什麼'@@ ROWCOUNT'需要一個命令?這是一個標量,它沒有秩序。 – 2014-09-19 10:13:01

0

試試這個:

--build sample data 

create table #temp_table(
    [Row] int, 
    [Id] varchar(200), 
    [Year] int, 
    [Period] int, 
    [Number] decimal(18,2), 
    [PeriodYear] date 
) 
insert into #temp_table 
select 49, '000000000130000000', 2014, 4, NULL, '4/1/2014' union all 
select 50, '000000000130000000', 2014, 3, 286.26, '3/1/2014' union all 
select 51, '000000000130000000', 2014, 2, NULL, '2/1/2014' union all 
select 52, '000000000130000000', 2014, 1, NULL, '1/1/2014' union all 
select 53, '000000000130000000', 2013, 12, 286.26, '12/1/2013' union all 
select 54, '000000000130000000', 2013, 11, NULL, '11/1/2013' union all 
select 55, '000000000130000000', 2013, 10, NULL, '10/1/2013' union all 
select 56, '000000000130000000', 2013, 9, 286.26, '9/1/2013' union all 
select 57, '000000000130000000', 2013, 8, NULL, '8/1/2013' union all 
select 58, '000000000130000000', 2013, 7, NULL, '7/1/2013' union all 
select 59, '000000000130000000', 2013, 6, 286.26, '6/1/2013' union all 
select 60, '000000000130000000', 2013, 5, NULL, '5/1/2013' union all 
select 61, '000000000130000000', 2013, 4, 286.26, '4/1/2013' union all 
select 62, '000000000130000000', 2013, 3, 291.98, '3/1/2013' union all 
select 63, '000000000130000000', 2013, 2, NULL, '2/1/2013' union all 
select 64, '000000000130000000', 2013, 1, 291.98, '1/1/2013' union all 
select 65, '000000000130000000', 2012, 12, 280.49, '12/1/2012' union all 
select 66, '000000000130000000', 2012, 11, 280.49, '11/1/2012' union all 
select 67, '000000000130000000', 2012, 10, 280.49, '10/1/2012' union all 
select 68, '000000000130000000', 2012, 9, 289.96, '9/1/2012' union all 
select 69, '000000000130000000', 2012, 8, NULL, '8/1/2012' union all 
select 70, '000000000130000000', 2012, 7, 289.96, '7/1/2012' union all 
select 71, '000000000130000000', 2012, 6, 294.54, '6/1/2012' union all 
select 72, '000000000130000000', 2012, 5, NULL, '5/1/2012' 

select * from #temp_table 

update t1 
    set t1.number = (select top 1 t2.number from #temp_table t2 where t2.number is not null and t2.row < t1.row order by t2.row desc) 
from #temp_table t1 
where number is null 

select * from #temp_table 


drop table #temp_table 
相關問題