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 |
SilentCodingOne
Starting Member
20 Posts |
Posted - 2008-12-30 : 22:55:49
|
I'm working on a homework problem that calls for me to use a CTE to analyze a table called NewEmployees which was added to the Northwind database. It is basically a copy of the Employee table structure. I have created a CTE which does pull in the data I'm looking to use in the report however all that comes up are the column headers . I have attached a sample look at what the report is suppose to look like and my current code. Any help would be greatly appreciated.USE NorthwindGOWITH EmployeeReport AS(SELECT ne.EmployeeID, ne.LastName, ne.FirstName, ne.Title, ne.ReportsTo AS ManagerID,e.LastName AS ManagerLastName, e.FirstName AS ManagerFirstName , 0 AS levelFROM dbo.NewEmployees ne JOINdbo.Employees e ON ne.ReportsTo = e.EmployeeIDWHERE ne.ReportsTo IS NULLUNION ALLSELECT ne.EmployeeID, ne.LastName, ne.FirstName, ne.Title, ne.ReportsTo AS ManagerID,e.LastName AS ManagerLastName, e.FirstName AS ManagerFirstName , ER.level + 1FROM dbo.NewEmployees ne JOINdbo.Employees e ON ne.ReportsTo = e.EmployeeIDINNER JOIN EmployeeReport ER ON ne.ReportsTo = ER.EmployeeID) SELECT * FROM EmployeeReport EmpID LName FName Level MngrID MngrLName MngrFName 13 Inoue George 1 1 Fuller Andrew 25 Talon Rob 1 1 Fuller Andrew 37 Evans Cleave 1 1 Fuller Andrew … 901 Young Ward 1 1 Fuller Andrew -- row 75 890 Normal Mark 2 901 Young Ward 891 Orthon Navid 2 901 Young Ward |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-31 : 03:20:23
|
try like thisUSE NorthwindGOWITH EmployeeReport AS(SELECT ne.EmployeeID, ne.LastName, ne.FirstName, ne.Title, ne.ReportsTo AS ManagerID,CAST(NULL AS Varchar(100)) AS ManagerLastName, CAST(NULL AS Varchar(100)) AS ManagerFirstName , 0 AS levelFROM dbo.NewEmployees ne WHERE ne.ReportsTo IS NULLUNION ALLSELECT ne.EmployeeID, ne.LastName, ne.FirstName, ne.Title, ne.ReportsTo AS ManagerID,e.LastName AS ManagerLastName, e.FirstName AS ManagerFirstName , ER.level + 1FROM dbo.NewEmployees ne JOINdbo.Employees e ON ne.ReportsTo = e.EmployeeIDINNER JOIN EmployeeReport ER ON ne.ReportsTo = ER.EmployeeID) SELECT * FROM EmployeeReport |
|
|
SilentCodingOne
Starting Member
20 Posts |
Posted - 2009-01-01 : 01:30:06
|
Thanks for the tips. The code did not work but it did give me some ideas. I modified the CTE to look like this:USE NorthwindGOWITH EmployeeReport AS(SELECT ne.EmployeeID, ne.LastName, ne.FirstName, ne.Title, 0 AS level, ne.ReportsTo ASManagerIDFROM dbo.NewEmployees ne UNION ALLSELECT ne.EmployeeID, ne.LastName, ne.FirstName, ne.Title, ER.level + 1, ne.ReportsTo ASManagerIDFROM dbo.NewEmployees ne INNER JOIN EmployeeReport ER ON ne.ReportsTo = ER.EmployeeID) SELECT er.EmployeeID, er.LastName, er.FirstName, er.level, er.ManagerID, mr.LastName AS ManagersLastName, mr.FirstName AS ManagersFirstName FROM EmployeeReport erINNER JOIN EmployeeReport mrON er.ManagerID = mr.EmployeeID This code brings back a number of records but it appears everything is duplicated. Any ideas as to where I'm going wrong? |
|
|
SilentCodingOne
Starting Member
20 Posts |
Posted - 2009-01-01 : 23:30:52
|
I was able to finally get it to work using something like this:USE NorthwindGOWITH EmployeeReport AS(SELECT ne.EmployeeID, ne.LastName, ne.FirstName, ne.Title, ne.ReportsTo AS ManagerID, e.LastName AS ManagersLastName, e.FirstName AS ManagersFirstName,0 AS levelFROM dbo.NewEmployees ne LEFT JOIN dbo.Employees e ON ne.EmployeeID = e.EmployeeIDUNION ALLSELECT ne.EmployeeID, ne.LastName, ne.FirstName, ne.Title, ne.ReportsTo AS ManagerID,e.LastName AS ManagerLastName, e.FirstName AS ManagerFirstName , ER.level + 1FROM dbo.NewEmployees ne JOINdbo.Employees e ON ne.ReportsTo = e.EmployeeIDINNER JOIN EmployeeReport ER ON ne.ReportsTo = ER.EmployeeID) SELECT DISTINCT er.EmployeeID, er.LastName, er.FirstName, er.level, er.ManagerID,mr.LastName AS ManagersLastName, mr.FirstName AS ManagersFirstName FROM EmployeeReport erINNER JOIN EmployeeReport mrON er.ManagerID = mr.EmployeeID |
|
|
|
|
|
|
|