| Author |
Topic |
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-05-26 : 10:13:37
|
| I have a database, that has multiple records, that can be updated at different times, and these are by date column.I wanted to know if there is a way i can run a query to look at all the records basked on the users username, and update a column with the data that was updated latest.So the query should run, look at all records that have the same username, then look at all the sis_datecreated, and look for the one that is the latest date, and then update the latest_rec_flag column with 'yes', by default that column is noTable Layoutid - sis_datecreate - username - latest_rec_flagTable Sample Data1 - 12:00 09/19/2000 - joe1 - no2 - 12:00 10/22/2000 - joe1 - no3 - 12:00 11/16/2010 - joe1 - no4 - 12:00 08/19/2000 - joe2 - no5 - 12:00 09/19/2000 - joe2 - noResults should look like1 - 12:00 09/19/2000 - joe1 - no2 - 12:00 10/22/2000 - joe1 - no3 - 12:00 11/16/2010 - joe1 - yes4 - 12:00 08/19/2000 - joe2 - no5 - 12:00 09/19/2007 - joe2 - yesAny help would be great, I am horrible at these types of queries. |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-05-26 : 10:26:49
|
| [code];with cteas(select flag,ROW_NUMBER()over(PARTITION by username order by sis_datecreate desc)as rid from yourtable )update cte set flag='yes' where rid=1select * from yourtable[/code]P.S PLEASE TEST IN TEST ENVIROMENT FIRSTPBUH |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-05-26 : 10:28:18
|
Try:update dtset latest_rec_flag='yes'from(select id, sis_datecreate, username, latest_rec_flag, rownumber() over (partition by username order by sis_datecreate desc) as rownum)dtwhere rownum=1 No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-05-26 : 10:38:34
|
quote: Originally posted by webfred Try:update dtset latest_rec_flag='yes'from(select id, sis_datecreate, username, latest_rec_flag, rownumber() over (partition by username order by sis_datecreate desc) as rownum)dtwhere rownum=1 No, you're never too old to Yak'n'Roll if you're too young to die.
I doubt it will work.My guess is it will update all the columns to 'Yes' as there is no relation with the derived table.PBUH |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-05-26 : 10:40:11
|
quote: Originally posted by Idera
quote: Originally posted by webfred Try:update dtset latest_rec_flag='yes'from(select id, sis_datecreate, username, latest_rec_flag, rownumber() over (partition by username order by sis_datecreate desc) as rownum)dtwhere rownum=1 No, you're never too old to Yak'n'Roll if you're too young to die.
I doubt it will work.My guess is it will update all the columns to 'Yes' as there is no relation with the derived table.PBUH
But there is a relation because I am updating directly the derived table  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-05-26 : 10:41:05
|
| Let me try.PBUH |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-05-26 : 10:48:06
|
Oop's my bad.It is working.Missed the derived table name in update part.Sorry.declare @tbl as table(id int,dt date,flag varchar(5))insert into @tblselect 1,GETDATE(),'no' union allselect 1,GETDATE()-1,'no'union allselect 2,GETDATE()-3,'no'union allselect 2,GETDATE()-4,'no'update dtset flag='yes'from (select id, flag,ROW_NUMBER() over (partition by id order by dt desc) as rownum from @tbl t1)dt where rownum=1select * from @tbl PBUH |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-05-26 : 10:49:49
|
no problem  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-05-26 : 11:01:11
|
| lol so what one do i use lol |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-05-26 : 11:02:58
|
They are nearly the same.To choose one is your problem  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|