繁体   English   中英

填补日期之间的空白

[英]Fill in gaps between dates

我需要在表中填写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 id并查看每个唯一ID的date(DOS)和CKD_Stage。 查看每个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

因此,我需要所有201509之前的NULL为2,而201509之后的所有NULL为3。我有1000条记录,因此我需要使用它来处理一系列不同的ID和不同的CKD_STAGES。

如果我理解正确,那么您希望将null值列设置为2(如果它在dos列值201509之前),而将3设置为在201509之后。

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

使用apply()获取给定x的上一个和下一个非null

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

extrester演示: 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 |
+--------+-----------+-----------+----------+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM