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 |
Adya1979
Starting Member
2 Posts |
Posted - 2010-07-23 : 04:32:10
|
Hi,I want to update column with the 1st row of same group. Below are the sample of data,Id Sur_No Sur_Type1 5 null1 3 null1 10 null2 3 null2 11 nullThis is the expected output,Id Sur_No Sur_Type1 5 51 3 null1 10 null2 3 32 11 nullThis is what i've done,UPDATE [myTable] SET [Sur_Type]=(SELECT TOP 1 Sur_No FROM [myTable]) and this is the output,Id Sur_No Sur_Type1 5 51 3 51 10 52 3 52 11 5any idea how to resolve this problem? thanks in advance :) |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-07-23 : 07:11:44
|
Try This:UPDATE [myTable] SET [Sur_Type]=Case When Subtab.Srno =1 then T.Sur_no else null endFrom [myTable] T join (Select Row_number() over (Partition by Id order by Sur_no) as Srno ,Id, sur_nofrom [myTable] ) As SubTabOn T.Id = SubTab.IDand T.Sur_no = Subtab.Sur_noExample:declare @Test table(Id int,Sur_no int,Sur_type int)Insert into @TestSelect 1,5, null unionSelect 1,3 ,null unionSelect 1,10 ,null unionSelect 2,3, null unionSelect 2,11, null Select * from @TestUpdate @Test set Sur_Type = Case When Subtab.Srno =1 then T.Sur_no else null endFrom @Test T join (Select Row_number() over (Partition by Id order by Sur_no) as Srno ,Id, sur_nofrom @Test ) As SubTabOn T.Id = SubTab.IDand T.Sur_no = Subtab.Sur_no Select * from @TestRegards,BohraI am here to learn from Masters and help new bees in learning. |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2010-07-23 : 07:17:42
|
quote: Originally posted by pk_bohra Try This:UPDATE [myTable] SET [Sur_Type]=Case When Subtab.Srno =1 then T.Sur_no else null endFrom [myTable] T join (Select Row_number() over (Partition by Id order by Sur_no) as Srno ,Id, sur_nofrom [myTable] ) As SubTabOn T.Id = SubTab.IDand T.Sur_no = Subtab.Sur_noExample:declare @Test table(Id int,Sur_no int,Sur_type int)Insert into @TestSelect 1,5, null unionSelect 1,3 ,null unionSelect 1,10 ,null unionSelect 2,3, null unionSelect 2,11, null Select * from @TestUpdate @Test set Sur_Type = Case When Subtab.Srno =1 then T.Sur_no else null endFrom @Test T join (Select Row_number() over (Partition by Id order by Sur_no) as Srno ,Id, sur_nofrom @Test ) As SubTabOn T.Id = SubTab.IDand T.Sur_no = Subtab.Sur_no Select * from @TestRegards,BohraI am here to learn from Masters and help new bees in learning.
Row_number() can't be available in Mssql 2000.Note: Posted in 2000 ForumSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2010-07-23 : 07:34:25
|
try this:create table Ad(id int,Snu int,Stype int)insert into Adselect 1, 5, null union allselect 1, 3, null union allselect 1, 10, null union allselect 2, 3, null union allselect 2, 11, null union allselect 3, 4, null union allselect 3, 1, null--(7 row(s) affected)select * from adupdate adset ad.stype = (select x.snu from ( (select ad.id ,ad.snu ,row_number() over (partition by id order by id) as R_N from ad)) as x where x.R_N = 1 and x.id = ad.id)from ad select * from addrop table ad |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-07-23 : 09:32:09
|
quote: Originally posted by senthil_nagoreRow_number() can't be available in Mssql 2000.Note: Posted in 2000 ForumSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/
I agree with you. I didn't noticed the version.ThanksI am here to learn from Masters and help new bees in learning. |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2010-07-24 : 02:10:20
|
this is SQL 2000 solution. You will see I added extra identity column which is in this case needed.create table Ad(id_identity int identity(1,1),id int,Snu int,Stype int)insert into Adselect 1, 5, null union allselect 1, 3, null union allselect 1, 10, null union allselect 2, 3, null union allselect 2, 11, null union allselect 3, 4, null union allselect 3, 1, null--(7 row(s) affected)update adset ad.stype = (select x.snu from ( select a1.id ,a1.snu ,a1.stype ,(select count(0)+1 from ad as a2 where a1.id = a2.id and a1.id_identity > a2.id_identity )as R_N from ad as a1 ) x where x.R_N = 1 and x.id = ad.id )from adselect * from addrop table ad |
|
|
Adya1979
Starting Member
2 Posts |
Posted - 2010-07-25 : 21:53:21
|
Hi all,Thanks for the reply..I'm using version 2000 and tested the code from slimt_slimt and it works But i will also try pk_bohra's coding in different version of sql. Thanks to all for your help..really appreciate it.. |
|
|
|
|
|
|
|