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 |
veparala
Starting Member
30 Posts |
Posted - 2008-02-19 : 08:23:06
|
Hi I have one table with the following values. SNo Empid FName LName 453 John Lorry 830 Steve Irina 298 David Dis SNo is empty in all rows. I need to pupulate Sno with row_number() function. Can anybody help me out?ThanksVenkat |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2008-02-19 : 08:25:28
|
ROW_NUMBER() function is available only in SQL Server 2005 and above.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
jackv
Master Smack Fu Yak Hacker
2179 Posts |
Posted - 2008-02-19 : 08:33:58
|
CREATE TABLE #temp(EmpId INT,FName VARCHAR(20),LName VARCHAR(20))INSERT INTO #temp SELECT 100,'aaa','aaa'UNIONSELECT 101,'bbb','bbb'UNIONSELECT 102,'ccc','ccc'UNIONSELECT 103,'ddd','ddd'UNIONSELECT 104,'eee','eee'SELECT EmpId,FName,LName,(select count(*) from #temp where Empid <> t.EmpID and EmpID<t.EmpID) as SNoFROM #temp tORDER BY EmpIdDROP TABLE #tempJack Vamvas--------------------Search IT jobs from multiple sources- http://www.ITjobfeed.com |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2008-02-19 : 08:38:52
|
quote: Originally posted by jackv CREATE TABLE #temp(EmpId INT,FName VARCHAR(20),LName VARCHAR(20))INSERT INTO #temp SELECT 100,'aaa','aaa'UNIONSELECT 101,'bbb','bbb'UNIONSELECT 102,'ccc','ccc'UNIONSELECT 103,'ddd','ddd'UNIONSELECT 104,'eee','eee'SELECT EmpId,FName,LName,(select count(*) from #temp where Empid <> t.EmpID and EmpID<t.EmpID) as SNoFROM #temp tORDER BY EmpIdDROP TABLE #tempJack Vamvas--------------------Search IT jobs from multiple sources- http://www.ITjobfeed.com
Please note that this method can be awefully slow on table with large number of records.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-19 : 08:39:05
|
quote: Originally posted by jackv CREATE TABLE #temp(EmpId INT,FName VARCHAR(20),LName VARCHAR(20))INSERT INTO #temp SELECT 100,'aaa','aaa'UNIONSELECT 101,'bbb','bbb'UNIONSELECT 102,'ccc','ccc'UNIONSELECT 103,'ddd','ddd'UNIONSELECT 104,'eee','eee'SELECT EmpId,FName,LName,(select count(*) +1 from #temp where Empid <> t.EmpID and EmpID<t.EmpID) as SNoFROM #temp tORDER BY EmpIdDROP TABLE #tempJack Vamvas--------------------Search IT jobs from multiple sources- http://www.ITjobfeed.com
You probably need to add 1 else SNo will start with 0. Also i really think the first condition check is redundant (it is implied by second check) |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-19 : 08:48:03
|
quote: Originally posted by veparala Hi I have one table with the following values. SNo Empid FName LName 453 John Lorry 830 Steve Irina 298 David Dis SNo is empty in all rows. I need to pupulate Sno with row_number() function. Can anybody help me out?ThanksVenkat
Where do you want to show data with serial number?MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|