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 2005 Forums
 Transact-SQL (2005)
 Update flag

Author  Topic 

amsqlguy
Yak Posting Veteran

89 Posts

Posted - 2009-06-12 : 09:44:44
Guys,

I have following data set for which I need to set flag based on the timestamp when the last document has been created.

EMPLOYEEID DATETIMECREATED RESIDENCECITY CURRENTRESIDENCEFLG
100 2009-06-12 CITYA Y
100 2008-06-12 CITYB N
100 2009-01-19 CITYC N

In the above example for the same employeeID 100 based on datetimecreated each night I should run a job to update the current flag, for employee ID 100
the current residence flag based on date time created is updated to Y for CITYA.

Is there any way to do this since I cannot use group by with max(id) function?

thanks





SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-12 : 09:52:27
UPDATE f
SET CURRENTRESIDENCEFLG = CASE recID WHEN 1 THEN 'Y' ELSE 'N' END
FROM (
SELECT CURRENTRESIDENCEFLG,
ROW_NUMBER() OVER (PARTITION BY EMPLOYEEID ORDER BY DATETIMECREATED DESC) AS recID
FROM Table1
) AS f


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -