SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Compare 2 consecutive rows in same table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

nitin1309
Starting Member

USA
3 Posts

Posted - 06/07/2012 :  13:47:45  Show Profile  Reply with Quote
Hello Everybody,

I need to compare 2 consecutive rows in the same table. If the data is missing in the second row, I should update it with the first row's data.

For Eg:
Row EmpID DATE PositionID EmpStatus EmpDept
------------------------------------------------------------------------
R1 21 2010-12-31 NULL TC 40
R2 21 2010-01-25 90156840101 NULL NULL
R3 21 2003-11-25 NULL AC NULL


First Iteration: Since Row1 EmpStatus = TC, I want to Update the EmpStatus on Row2 to TC (Since its NULL) and EmployeeDept to 40 on Row2.

Row EmpID DATE PositionID EmpStatus EmpDept
------------------------------------------------------------------------
R1 21 2010-12-31 NULL TC 40
R2 21 2010-01-25 90156840101 TC 40
R3 21 2003-11-25 NULL AC NULL

Second Iteration: Since PositionID is NULL on Row3, I wanna update Row3 with the PositionID of Row2. Since Row2 now has EmpStatus = TC, I want to compare the row2 and row3 data. Since Row3 has a new value, I wanna retain the new value = AC. But at the same time I wanna update the value of EmpDept of Row3 = 40 since its NULL


Row EmpID DATE PositionID EmpStatus EmpDept
------------------------------------------------------------------------
R1 21 2010-12-31 NULL TC 40
R2 21 2010-01-25 90156840101 TC 40
R3 21 2003-11-25 90156840101 AC 40


I am working on historical data load and I have to build records backwards.

Can anyone please tell me how to code this ?
I wanna know if we can do this updates preferable without using cursors as I have a lot of employees in this table.

Thanks a lot!


nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 06/07/2012 :  13:56:34  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
with cte as
(
select *, row_number() over (order by EmpID, DATE ) frrom tbl
)
update cte
set PositionID = t2.PositionID
from cte
join cte t2
on cte.seq = t2.seq+1
where cte.PositionID is null
and t2.PositionID is not null

Might have to run it a few times if you have consecutive rows with nulls.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

nitin1309
Starting Member

USA
3 Posts

Posted - 06/07/2012 :  14:09:25  Show Profile  Reply with Quote
Thanks nigel!
But I need to update this continuously in one pass. This code will have to go into a store proc and can only be run once.

Is there any other way, we can implement this.

Thanks!
Go to Top of Page

vinu.vijayan
Posting Yak Master

India
227 Posts

Posted - 06/14/2012 :  06:43:15  Show Profile  Reply with Quote
Nigel's Query may be modified according to your requirement. What Logic should be applied if there are multiple rows containing NULL for EmpStatus and PositionId?

N 28° 33' 11.93148"
E 77° 14' 33.66384"
Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 06/14/2012 :  06:58:20  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
You can have a loop in a stored procedure.


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000