2013-11-25 104 views
2

我正在努力解決以下問題。考慮下面發佈的示例表。我需要做的是更新表,特別是每個行上的NULL值與「最後」非NULL值。例如,對行3和4中的NULL值應與同一列的2行中的值進行更新,即對行9至15與8行中的值更新並用最後一個非空值填充空行值 - SQL表

2 007585102 2001 03 31 2001 04 12 2 154980 6300 154980 6300 
3 007585102 2001 03 31 2001 04 19 2 154980 6300 154980 6300 
4 007585102 2001 03 31 2001 04 26 2 154980 6300 154980 6300 

和NULL值等等。 我真的不知道如何做到這一點,我將不勝感激任何幫助。提前致謝。

對不起,關於極其糟糕的表格格式,但我不能發佈任何東西,但純文本。

示例表

1 007585102 2001 03 31 2001 04 05 2 543660 22100 543660 22100 
2 007585102 2001 03 31 2001 04 12 2 154980 6300 154980 6300 
3 007585102  NULL  2001 04 19 NULL  NULL NULL  NULL  NULL 
4 007585102  NULL  2001 04 26 NULL  NULL NULL  NULL  NULL 
5 007585102 2001 03 31 2001 05 03 2 2726664 110840 2726664 110840 
6 007585102 2001 03 31 2001 05 10 2 836400 34000 836400 34000 
7 007585102 2001 03 31 2001 05 17 2 534804 21740 7634364 310340 
8 007585102 2001 03 31 2001 05 24 2 4920  200  4920  200 
9 007585102  NULL  2001 05 31 NULL  NULL NULL  NULL NULL  
10 007585102  NULL  2001 06 07 NULL  NULL NULL  NULL NULL 
11 007585102  NULL  2001 06 14 NULL  NULL NULL  NULL NULL 
12 007585102  NULL  2001 06 21 NULL  NULL NULL  NULL NULL 
13 007585102  NULL  2001 06 28 NULL  NULL NULL  NULL NULL 
14 007585102  NULL  2001 07 05 NULL  NULL NULL  NULL NULL 
15 007585102  NULL  2001 07 12 NULL  NULL NULL  NULL NULL 
16 007585102 2001 06 30 2001 07 19 2 2693301 118300 2693301 118300 
17 007585102 2001 06 30 2001 07 26 2 232220 10200 NULL NULL 

回答

0

我已經在這裏詳細解釋這一點: https://koukia.ca/common-sql-problems-filling-null-values-with-preceding-non-null-values-ad538c9e62a6#.k0dxirgwu 這裏是TSQL需要,

SELECT * 
INTO #Temp 
FROM ImportedSales; 

;With CTE 
As 
(
    SELECT ProductName 
      , Id 
      , COUNT(ProductName) OVER(ORDER BY Id ROWS UNBOUNDED PRECEDING) As MyGroup 
    FROM #Temp 
), 
GetProduct 
AS 
(
    SELECT [ProductName] 
      , First_Value(ProductName) OVER(PARTITION BY MyGroup ORDER BY Id ROWS UNBOUNDED PRECEDING) As UpdatedProductName 
    FROM CTE 
) 

UPDATE GetProduct 
Set ProductName = UpdatedProductName; 

SELECT * 
FROM #TemP; 
+0

如何實現在SQL Server 2008 R2相同的結果?據我瞭解,自2012年起,ROWS UNBOUNDED PRECEDING條款被包含在內。 –

+0

您可以使用此處發佈的CTE解決方案:https://koukia.ca/common-sql-problems-filling-null-values-with-preceding-non-null-values-ad538c9e62a6#.k0dxirgwu – Aram

2

我不是很驕傲我的答案,但至少它的工作原理。自行尋找更優雅的方式。我建議遞歸cte。

drop table #temp 
GO 
select 
    * 
into #temp 
from (
    select 1 as id, '2001 03 31' as dat union all 
    select 2, '2001 03 31' union all 
    select 3, null union all 
    select 4, null union all 
    select 5, '2001 03 31' union all 
    select 6, '2001 03 31' union all 
    select 7, '2001 03 31' union all 
    select 8, '2001 03 31' union all 
    select 9, null union all 
    select 10, null union all 
    select 11, null union all 
    select 12, null union all 
    select 13, null union all 
    select 14, null union all 
    select 15, null union all 
    select 16, '2001 06 30' union all 
    select 17, '2001 06 30' 
) x 

update t 
set 
    t.dat = t2.dat 
from #temp t 
join (
    select 
     t1.id, max(t2.id) as maxid 
    from #temp t1 
    join #temp t2 
     on t1.id>t2.id 
     and t2.dat is not null 
     and t1.dat is null 
    group by 
     t1.id 
) x 
    on t.id=x.id 
join #temp t2 
    on t2.id=x.maxid 

select * from #temp