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.
| Author |
Topic |
|
sqlpal2007
Posting Yak Master
200 Posts |
Posted - 2009-11-18 : 10:05:22
|
Hello All,I have to update a flag in the details table only on one row where the criteria matches.I have to set SFlag = 1 for the minimum PoleConnection number and rest of the line items should have SFlag = 0 for that PoleID and PoleIdentity.Here is an example:PoleID PoleConnection PoleIdentity SFlag100 2122 'A0019' 1100 6572 'A0019' 1100 1220 'A0019' 1450 4590 'BC5009' 0561 3244 'DC0019' 1561 400 'DC0019' 0output-PoleID PoleConnection PoleIdentity SFlag100 2122 'A0019' 0100 6572 'A0019' 0100 1220 'A0019' 1450 4590 'BC5009' 1561 3244 'DC0019' 0561 400 'DC0019' 1 Thanks,-S |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2009-11-18 : 14:24:03
|
| [code]create table ##pole (PoleID int, PoleConnection int,PoleIdentity varchar(10), SFlag bit)insert into ##pole (PoleID,PoleConnection,PoleIdentity,SFlag)select100 , 2122 , 'A0019' , 1 union all select100 , 6572 , 'A0019' , 1 union all select100 , 1220 , 'A0019' , 1 union all select450 , 4590 , 'BC5009' , 0 union all select561 , 3244 , 'DC0019' , 1 union all select561 , 400 , 'DC0019' , 0 Step 1, remove all the flagsUpdate ##pole set SFlag = 0 Step 2, Put in the flagsUpdate ##pole set SFlag = 1from ##pole pinner join(select PoleID,PoleConnection,PoleIdentity ,ROW_NUMBER() OVER (PARTITION BY PoleIdentity ORDER BY PoleConnection ASC) as RN from ##pole) xon p.PoleConnection= x.PoleConnection and p.PoleID= x.PoleIDand p.PoleIdentity = x.PoleIdentitywhere RN = 1select * from ##poledrop table ##pole output-PoleID PoleConnection PoleIdentity SFlag100 2122 'A0019' 0100 6572 'A0019' 0100 1220 'A0019' 1450 4590 'BC5009' 1561 3244 'DC0019' 0561 400 'DC0019' 1[/code]http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
|
|
|
|
|