how do i get output as like below from the below query
Headname | Name |
---|---|
Fabro | Canon |
Estacio | |
Fajardo | |
Sobrevilla | |
Teruel | Alabaso |
Bahadur | |
Casia | |
jayan | |
Tano |
-- code begin here
create Table #Employee
(
Empid int primary key identity(1,1),
Name varchar(255),
Designation varchar(255),
status int
)
insert into #Employee(Name,Designation,status) values('jayan','Driver',1)
insert into #Employee(Name,Designation,status) values('Casia','Mechanic',1)
insert into #Employee(Name,Designation,status) values('Alabaso','Mechanic',1)
insert into #Employee(Name,Designation,status) values('Bahadur','Mechanic',1)
insert into #Employee(Name,Designation,status) values('Tano','Mechanic',1)
insert into #Employee(Name,Designation,status) values('Canon','Helper',1)
insert into #Employee(Name,Designation,status) values('Teruel','Supervisor',1)
insert into #Employee(Name,Designation,status) values('Estacio','Driver',1)
insert into #Employee(Name,Designation,status) values('Fabro','Supervisor',1)
insert into #Employee(Name,Designation,status) values('Fajardo','Driver',1)
insert into #Employee(Name,Designation,status) values('Sobrevilla','Driver',1)
create table #DeptHead
(
HeadID int primary key identity(1,1),
HeadName varchar(200),
Fk_Empid int FOREIGN KEY REFERENCES #Employee(Empid),
deptID int,
Deptname varchar(200),
status int
)
insert into #DeptHead(HeadName,Fk_Empid,deptID,Deptname,status) values('Teruel',7,1,'Workshop',1)
insert into #DeptHead(HeadName,Fk_Empid,deptID,Deptname,status) values('Fabro',9,2,'Operation',1)
create table #ReportingMgr
(
Id int primary key identity(1,1),
Fk_HeadId int FOREIGN KEY REFERENCES #DeptHead(HeadID),
FK_Empid int FOREIGN KEY REFERENCES #Employee(Empid),
Status int
)
insert into #ReportingMgr(Fk_HeadId,Fk_Empid,Status) values(7,1,1)
insert into #ReportingMgr(Fk_HeadId,Fk_Empid,Status) values(7,2,1)
insert into #ReportingMgr(Fk_HeadId,Fk_Empid,Status) values(7,3,1)
insert into #ReportingMgr(Fk_HeadId,Fk_Empid,Status) values(7,4,1)
insert into #ReportingMgr(Fk_HeadId,Fk_Empid,Status) values(7,5,1)
insert into #ReportingMgr(Fk_HeadId,Fk_Empid,Status) values(9,6,1)
insert into #ReportingMgr(Fk_HeadId,Fk_Empid,Status) values(9,8,1)
insert into #ReportingMgr(Fk_HeadId,Fk_Empid,Status) values(9,10,1)
insert into #ReportingMgr(Fk_HeadId,Fk_Empid,Status) values(9,11,1)
select dense_rank() over(order by HeadName) as rowid, HeadName,Name,Empid from #DeptHead d
inner join
#ReportingMgr r on d.Fk_Empid = r.Fk_HeadId
inner join
#Employee e on e.Empid = R.Fk_Empid
group by HeadName,Name,Empid
drop table #Employee
drop table #DeptHead
drop table #ReportingMgr