2017-05-05 132 views
0

我需要填寫表中的NULL空位。每個ID(x)可以有多個CKD_STAGE,但有幾個月的空值,我想用最新的已知CKD_STAGE來填充它們之間的空格。這是迄今爲止我所擁有的。填寫日期間的差距

SELECT DISTINCT DOS, x, CKD_STAGE, REF_YEAR 
FROM #CKD_MM 

DECLARE @MAXDOS INT = (SELECT MAX(DOS) FROM #CKD_MM) 
DECLARE @MINDOS INT = (SELECT MIN(DOS) FROM #CKD_MM) 

Update #CKD_MM 
set CKD_STAGE = @MAXDOS 
WHERE CKD_STAGE is null 
     AND DOS BETWEEN @MAXDOS AND @MINDOS 

我的邏輯是要找到不同的X標識,並期待在日期(DOS)和CKD_Stage每個唯一ID。查看每個ckd_stage的最大和最小日期,並用最大值填充它們之間的間隔。該程序運行,但我仍然獲得NULL值。

這裏是什麼樣的表看起來像一個快照的ID的

DOS  x   CKD_STAGE REF_YEAR 
201405 480000000 2  2014 
201510 480000000 NULL 2015 
201504 480000000 NULL 2015 
201506 480000000 NULL 2015 
201512 480000000 NULL 2015 
201511 480000000 NULL 2015 
201409 480000000 2  2014 
201509 480000000 3  2015 
201507 480000000 NULL 2015 
201404 480000000 NULL 2014 
201501 480000000 NULL 2015 
201411 480000000 NULL 2014 
201402 480000000 NULL 2014 
201503 480000000 NULL 2015 

所以我需要所有日期的NULL 201509前爲2,畢竟201509爲3。我有1000的的記錄,所以我需要這個工作在一系列不同的ID和不同的CKD_STAGES。

回答

0

如果我理解正確的話,你要設置的空值列是2,如果它是DOS列值201509和3之前如果是使用後向apply()得到以前201509.

Update #CKD_MM 
set CKD_STAGE = case when dos>=201509 then 3 else 2 end 
WHERE CKD_STAGE is null 
+0

不,我需要它的工作在所有範圍內的日期和CKD_STAGE價值觀,我有記錄的100的,上面的表格只是一個快照1記錄(一個ID)。 –

+0

您可以爲這些範圍值編寫case語句。您可以提供您的場景,以便我可以幫助您解決您的值範圍 – Rams

0

和下一非null值對於給定的x

select 
    t.dos 
    , t.x 
    , coalesce(t.ckd_stage,prev.ckd_stage,nxt.ckd_stage) as ckd_stage 
    , t.ref_year 
from t 
    outer apply (
    select top 1 i.ckd_stage 
    from t i 
    where i.x = t.x 
     and i.dos > t.dos 
     and i.ckd_stage is not null 
    order by i.dos asc 
) nxt 
outer apply (
    select top 1 i.ckd_stage 
    from t i 
    where i.x = t.x 
     and i.dos < t.dos 
     and i.ckd_stage is not null 
    order by i.dos desc 
) prev 
order by t.dos 

作爲update

update t 
set ckd_stage = coalesce(prev.ckd_stage,nxt.ckd_stage) 
from t 
    outer apply (
    select top 1 i.ckd_stage 
    from t i 
    where i.x = t.x 
     and i.dos > t.dos 
     and i.ckd_stage is not null 
    order by i.dos asc 
) nxt 
outer apply (
    select top 1 i.ckd_stage 
    from t i 
    where i.x = t.x 
     and i.dos < t.dos 
     and i.ckd_stage is not null 
    order by i.dos desc 
) prev 
where t.ckd_stage is null 

select * 
from t 
order by dos 

rextester演示:http://rextester.com/YFTM98118

回報:

+--------+-----------+-----------+----------+ 
| dos |  x  | ckd_stage | ref_year | 
+--------+-----------+-----------+----------+ 
| 201402 | 480000000 | 2   |  2014 | 
| 201404 | 480000000 | 2   |  2014 | 
| 201405 | 480000000 | 2   |  2014 | 
| 201409 | 480000000 | 2   |  2014 | 
| 201411 | 480000000 | 2   |  2014 | 
| 201501 | 480000000 | 2   |  2015 | 
| 201503 | 480000000 | 2   |  2015 | 
| 201504 | 480000000 | 2   |  2015 | 
| 201506 | 480000000 | 2   |  2015 | 
| 201507 | 480000000 | 2   |  2015 | 
| 201509 | 480000000 | 3   |  2015 | 
| 201510 | 480000000 | 3   |  2015 | 
| 201511 | 480000000 | 3   |  2015 | 
| 201512 | 480000000 | 3   |  2015 | 
+--------+-----------+-----------+----------+ 
+0

這不起作用,它仍然生成NULL值。 –

+0

@ C.AJ將我以前的答案合併爲一個。 – SqlZim