views:

29

answers:

2

Hello All

I would like to update a column in Table with reference to other column(s) in same table.

Ex: As in figure below - I would like to update effective endate with min date whichever is greater than effective Start Date of currrent record.

How can this be acheived in T-SQL. Can this be done with single update statement ?

alt text

Thanks.

+1  A: 

I suspect what you want can be accomplished with a case statement, but I can't give you code because the following part of your question makes no sense:

update effective endate with min date whichever is greater than effective Start Date of currrent record.

Joel Coehoorn
Hello Joel, min date mean min start date for given NK that is greater than current record start date. Thanks
Nev_Rahd
A: 

How about just using a subselect, something like the following:

UPDATE MyTable mt
SET mt.EFFECTIVE_END_DATETIME = (SELECT MIN(mt2.EFFECTIVE_START_DATETIME)
                                 FROM MyTable mt2
                                 WHERE mt2.EFFECTIVE_START_DATETIME > mt.EFFECTIVE_START_DATETIME);
lc
Thanks Lc, it worked changed it as UPDATE T0SET T0.EFFECTIVE_END_DATETIME = ( SELECT ISNULL(MIN(T1.EFFECTIVE_START_DATETIME),'9999-12-31') FROM SHARED.OSP_GEO_BOUNDARY_DIM T1 WHERE T1.EFFECTIVE_START_DATETIME > T0.EFFECTIVE_START_DATETIME AND T0.OSP_nk = T1.OSP_NK )FROM SHARED.OSP_GEO_BOUNDARY_DIM T0
Nev_Rahd