Generating a Serial Numbers through a Query

vishalneeraj-24503
Posted by vishalneeraj-24503 under Sql Server category on | Points: 40 | Views : 387
if(object_id('tempdb..#Employee') is not null)
drop table #Employee;

create table #Employee
(Employee_Name NVarchar(50))

INSERT #Employee (Employee_Name)
VALUES ('Vishal')
INSERT #Employee (Employee_Name)
VALUES ('Kumar')
INSERT #Employee (Employee_Name)
VALUES ('Vinod')
INSERT #Employee (Employee_Name)
VALUES ('Nitin')
INSERT #Employee (Employee_Name)
VALUES ('Vilas')
INSERT #Employee (Employee_Name)
VALUES ('Vipin')
INSERT #Employee (Employee_Name)
VALUES ('Manmeet')
INSERT #Employee (Employee_Name)
VALUES ('Neeraj')
INSERT #Employee (Employee_Name)
VALUES ('Lokesh')

--Now generae serial nos. as:-
select Serail_No = count(*),emp1.Employee_Name
from #Employee emp1 INNER JOIN #Employee emp2
ON emp1.Employee_Name >= emp2.Employee_Name
group by emp1.Employee_Name
order by Serail_No
drop table #Employee;

if(object_id('tempdb..#Employee') is not null)
drop table #Employee;


Output:-
1 Kumar
2 Lokesh
3 Manmeet
4 Neeraj
5 Nitin
6 Vilas
7 Vinod
8 Vipin
9 Vishal

Comments or Responses

Login to post response