Author |
Topic |
nirnir2
Starting Member
20 Posts |
Posted - 2014-05-26 : 02:35:11
|
I have a table Table1 with fieldsserialNum , integerType : integerType , serialNum1 , 11 , 22 , 32 , 43 , 103 , Null3 , Null4 , 204 , 215 , 225 , 40I need to update all rows where serialNum=null and Type=3 with the consecutive max(serialNum)The two empty rows should be3 , 413 , 42whats the most efficient way to do that. |
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-05-26 : 03:12:05
|
[code];with aCTEas (select 1 Type , 1 serialNum union all select 1 , 2 union all select 2 , 3 union all select 2 , 4 union all select 3 , 10 union all select 3 , Null union all select 3 , Null union all select 4 , 20 union all select 4 , 21 union all select 5 , 22 union all select 5 , 40), bCTEAS (select Type ,serialNum ,row_number() OVER(Order by type,serialNum) as rn from aCTE A where A.Type =3 AND A.serialNum IS NULL )select [type] , b.maxx+ rn as newSerialNumfrom bCTE cross join (select max(serialNum) as Maxx from aCTE B ) B[/code]output:[code]type SerialNum3 413 42[/code]sabinWeb MCP |
|
|
nirnir2
Starting Member
20 Posts |
Posted - 2014-05-26 : 03:22:03
|
quote: Originally posted by stepson
;with aCTEas (select 1 Type , 1 serialNum union all select 1 , 2 union all select 2 , 3 union all select 2 , 4 union all select 3 , 10 union all select 3 , Null union all select 3 , Null union all select 4 , 20 union all select 4 , 21 union all select 5 , 22 union all select 5 , 40), bCTEAS (select Type ,serialNum ,row_number() OVER(Order by type,serialNum) as rn from aCTE A where A.Type =3 AND A.serialNum IS NULL )select [type] , b.maxx+ rn as newSerialNumfrom bCTE cross join (select max(serialNum) as Maxx from aCTE B ) B output:type SerialNum3 413 42 sabinWeb MCP
Thanks but all I need is to update the empty SerialNum rows.I dont need to select anything, it was only example of the new values for those rows .this is big table with 300,000 rows . |
|
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-05-26 : 03:40:02
|
[code];with bCTEAS (select Type ,serialNum ,row_number() OVER(Order by type,serialNum) as rn ,B.Maxx from tblSerialNum A cross join (select max(serialNum) as Maxx from tblSerialNum B ) Bwhere A.Type =3 AND A.serialNum IS NULL )update bCTEset serialNum = Maxx +rnwhere Type =3 AND serialNum IS NULL[/code]sabinWeb MCP |
|
|
nirnir2
Starting Member
20 Posts |
Posted - 2014-05-26 : 06:58:23
|
Thanks |
|
|
|
|
|