fill in a null cell with cell from previous record

旧街凉风 提交于 2019-12-11 04:23:41

问题


Hi I am using DB2 sql to fill in some missing data in the following table:

Person       House     From           To      
------       -----     ----           --
1            586       2000-04-16     2010-12-03 
2            123       2001-01-01     2012-09-27
2            NULL      NULL           NULL
2            104       2004-01-01     2012-11-24
3            987       1999-12-31     2009-08-01
3            NULL      NULL           NULL

Where person 2 has lived in 3 houses, but the middle address it is not known where, and when. I can't do anything about what house they were in, but I would like to take the previous house they lived at, and use the previous To date to replace the NULL From date, and use the next address info and use the From date to replace the null To date ie.

Person       House     From           To      
------       -----     ----           --
1            586       2000-04-16     2010-12-03 
2            123       2001-01-01     2012-09-27
2            NULL      2012-09-27     2004-01-01
2            104       2004-01-01     2012-11-24
3            987       1999-12-31     2009-08-01
3            NULL      2009-08-01     9999-01-01

I understand that if there is no previous address before a null address, that will have to stay null, but if a null address is the last know address I would like to change the To date to 9999-01-01 as in person 3.

This type of problem seems to me where set theory no longer becomes a good solution, however I am required to find a DB2 solution because that's what my boss uses!

any pointers/suggestions welcome.

Thanks.


回答1:


It might look something like this:

select 
 person,
 house,
 coalesce(from_date, prev_to_date) from_date,
 case when rn = 1 then coalesce (to_date, '9999-01-01')
  else coalesce(to_date, next_from_date) end to_date
from
(select person, house, from_date, to_date,
 lag(to_date) over (partition by person order by from_date nulls last) prev_to_date,
 lead(from_date) over (partition by person order by from_date nulls last) next_from_date,
 row_number() over (partition by person order by from_date desc nulls last) rn
 from temp
) t

The above is not tested but it might give you an idea.

I hope in your actual table you have a column other than to_date and from_date that allows you to order rows for each person, otherwise you'll have trouble sorting NULL dates, as you have no way of knowing the actual sequence.




回答2:


create table Temp
(
 person varchar(2),
 house int,
 from_date date,
 to_date date 
)

insert into temp values 
(1,586,'2000-04-16','2010-12-03 '),
(2,123,'2001-01-01','2012-09-27'),
(2,NULL,NULL,NULL),
(2,104,'2004-01-01','2012-11-24'),
(3,987,'1999-12-31','2009-08-01'),
(3,NULL,NULL,NULL)


select  A.person,
        A.house,
        isnull(A.from_date,BF.to_date) From_date,
        isnull(A.to_date,isnull(CT.From_date,'9999-01-01')) To_date
from 
((select *,ROW_NUMBER() over (order by (select 0)) rownum from Temp) A left join
 (select *,ROW_NUMBER() over (order by (select 0)) rownum from Temp) BF
 on A.person = BF.person and
 A.rownum = BF.rownum + 1)left join
 (select *,ROW_NUMBER() over (order by (select 0)) rownum from Temp) CT 
  on A.person = CT.person and
  A.rownum = CT.rownum - 1


来源:https://stackoverflow.com/questions/17293073/fill-in-a-null-cell-with-cell-from-previous-record

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!