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)
 Sql Query Example- should i use Max?

Author  Topic 

Looper
Yak Posting Veteran

68 Posts

Posted - 2009-10-28 : 11:55:51
I have a table that holds a list of car leases for a user. A user can have multiple
car leases with start and end dates. What I want is a sql query that will set the lease end date to the current date for the record
that has the latest start date only. so if a user had two rows in this table I only want to update the row with the start date that is the latest.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-10-28 : 12:03:18
Something like this:
update car_leases
set lease_end_date = getdate()
where userid = wanted_userid
and lease_start_date=(select max(lease_start_date) from car_leases where userid=wanted_userid)



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -