Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Want to get last updated row

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2012-10-30 : 15:56:53
Each time the record is updated it saves the following detail the first three fields info, now i want to know when was the record updated prior to this one: where i pass the first 3 values. which is
I pass '168',12,'2012-OCT-25'

prior to 25th OCT record was updated on 18th OCT 2012.

that infomration i need to get, is it possible, i want to implement this logic within a loop and find out all looped data.


Declare @Sample table (acct_unit varchar(10),OBJ_ID int, STAT_DATE datetime, STATUS_FLAG varchar(20), SUR_KEY Int)
insert @Sample
select '168',12,'2012-OCT-25', 1234 union all
select '168',12,'2012-OCT-18', 1118 union all
select '168',12,'2012-OCT-16', 1103 union all
select '168',12,'2012-OCT-12', 1090 union all
select '173',18,'2012-OCT-25', 1220 union all
select '173',18,'2012-OCT-18', 1214 union all
select '173',18,'2012-OCT-16', 1190 union all
select '173',18,'2012-OCT-12', 1145




Thanks a lot for the helpful info.

chadmat
The Chadinator

1974 Posts

Posted - 2012-10-30 : 16:05:24
SELECT TOP 1 * FROM @Sample
WHERE STAT_DATE < @passedInDate
AND acct_unit = @passedInUnit
AND OBJ_ID = @passedInObjID

-Chad
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-10-30 : 16:07:38
With an order by clause, of course :)
quote:
Originally posted by chadmat

SELECT TOP 1 * FROM @Sample
WHERE STAT_DATE < @passedInDate
AND acct_unit = @passedInUnit
AND OBJ_ID = @passedInObjID
ORDER BY STAT_DATE DESC

-Chad

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-30 : 16:12:25
Make sure you pass date value in unambiguos format like YYYYMMDD

see

http://visakhm.blogspot.com/2011/12/why-iso-format-is-recommended-while.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2012-10-30 : 16:37:46
quote:
Originally posted by sunitabeck

With an order by clause, of course :)
quote:
Originally posted by chadmat

SELECT TOP 1 * FROM @Sample
WHERE STAT_DATE < @passedInDate
AND acct_unit = @passedInUnit
AND OBJ_ID = @passedInObjID
ORDER BY STAT_DATE DESC

-Chad






Thanks,

Forgot the Order By.

-Chad
Go to Top of Page

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2012-10-30 : 17:14:25
Thank you all for the great help.
Go to Top of Page
   

- Advertisement -