Click here to Skip to main content
15,891,657 members
Home / Discussions / Database
   

Database

 
GeneralRe: How to get data from OracleDatabase to SQL Server automaticly Pin
hmanhha2-Mar-16 3:53
hmanhha2-Mar-16 3:53 
GeneralRe: How to get data from OracleDatabase to SQL Server automaticly Pin
hmanhha2-Mar-16 23:11
hmanhha2-Mar-16 23:11 
GeneralRe: How to get data from OracleDatabase to SQL Server automaticly Pin
CHill602-Mar-16 23:44
mveCHill602-Mar-16 23:44 
GeneralRe: How to get data from OracleDatabase to SQL Server automaticly Pin
hmanhha3-Mar-16 16:02
hmanhha3-Mar-16 16:02 
QuestionOrder by by using levels using sql Script Pin
indian14322-Feb-16 10:55
indian14322-Feb-16 10:55 
AnswerRe: Order by by using levels using sql Script Pin
Mathi Mani22-Feb-16 14:37
Mathi Mani22-Feb-16 14:37 
GeneralRe: Order by by using levels using sql Script Pin
CHill6025-Feb-16 5:07
mveCHill6025-Feb-16 5:07 
AnswerRe: Order by by using levels using sql Script Pin
CHill6025-Feb-16 5:05
mveCHill6025-Feb-16 5:05 
You can use a recursive CTE to traverse the employee hierarchy - there a good example on the MSDN site - Recursive Queries Using Common Table Expressions[^]

Yours is a little more complicated because you have the extra table, which could have been replaced by a single column on the Employee table "ReportsTo". It is also a little more complicated because you want the Managers in Name order followed by the Employees (for each Manager) also in Name order.

This query seems to do what you want (I've used the sample data provided in the earlier response)
SQL
;WITH emps AS
(
	SELECT e.Id, e.[Name], e.[Level], er.ManagerId as ReportsTo, em.Name as ManagerName
	FROM Employee e
	left outer join employeeRelationship  er on er.EmployeeId = e.id
	left outer join employee em on er.ManagerId = em.id
), Emp_CTE AS (
	-- Anchor member - get only the managers
	SELECT Id, [Name], [Level], ReportsTo,
	MtoE = CAST(CAST(isnull(ManagerName,'') AS CHAR(30)) + '/' + CAST([Name] AS CHAR(30)) AS VARCHAR(MAX))
	FROM emps e
	WHERE ReportsTo IS NULL

	UNION ALL

	-- Recursive member - get the people who report to the managers
	SELECT e.Id, e.[Name], e.[Level], e.Id as ReportsTo,
    MtoE = MtoE + '/' + CAST(e.[Name] AS CHAR(30))

	FROM emps e
	INNER JOIN Emp_CTE ecte ON ecte.Id = e.ReportsTo
)
SELECT Id, [Name], [Level]
FROM Emp_CTE
order by MtoE

Giving the results you asked for:
C#
Id      Name    Level
3	dddd	1
9	ef	2
8	ff	2
2	nnnn	1
7	az	2
6	za	2
1	zzzz	1
5	bc	2
4	df	2

The explanation:
The first CTE emps is just to simplify the table structure as I can't use a left outer join in the recursive CTE that follows it. If you run that query by itself you get the following
C#
Id      Name    Level   ReportTo ManagerName
1	zzzz	1	NULL	NULL
2	nnnn	1	NULL	NULL
3	dddd	1	NULL	NULL
4	df	2	1	zzzz
5	bc	2	1	zzzz
6	za	2	2	nnnn
7	az	2	2	nnnn
8	ff	2	3	dddd
9	ef	2	3	dddd

The second CTE Emp_CTE is a recursive query that first gets only the Managers (marked as the "Anchor member" in the comment in the code). Those results are then fed into the "Recursive member" of the CTE until all of the employees have been processed.

The "clever" bit is the derived column MtoE that "tracks" the levels of recursion, capturing first the Manager's name and then the Workers' name. I've cast the individual names to CHAR(30) to ensure that the subsequent ORDER BY works properly - i.e. making sure all of the names are the same length, padded by spaces if necessary. You might need to change the 30 to a larger number if you have some long names. Also note in the Anchor member I've cast the combination of names to be a VARCHAR as the column grows in length the further down the recursion you have to go (e.g. if there was a level 3 employee).

The very last bit just captures the data from Emp_CTE and ORDERs it BY the derived column so the output appears in name order.
QuestionRecursive Common Table Expressions Pin
Amr.Mohammad8720-Feb-16 9:10
Amr.Mohammad8720-Feb-16 9:10 
SuggestionRe: Recursive Common Table Expressions Pin
CHill6020-Feb-16 9:43
mveCHill6020-Feb-16 9:43 
GeneralRe: Recursive Common Table Expressions Pin
Amr.Mohammad8720-Feb-16 11:21
Amr.Mohammad8720-Feb-16 11:21 
GeneralRe: Recursive Common Table Expressions Pin
CHill6020-Feb-16 23:57
mveCHill6020-Feb-16 23:57 
GeneralRe: Recursive Common Table Expressions Pin
Amr.Mohammad8721-Feb-16 7:04
Amr.Mohammad8721-Feb-16 7:04 
GeneralRe: Recursive Common Table Expressions Pin
Jörgen Andersson21-Feb-16 7:31
professionalJörgen Andersson21-Feb-16 7:31 
GeneralRe: Recursive Common Table Expressions Pin
CHill6021-Feb-16 8:04
mveCHill6021-Feb-16 8:04 
GeneralRe: Recursive Common Table Expressions Pin
Jörgen Andersson21-Feb-16 21:34
professionalJörgen Andersson21-Feb-16 21:34 
GeneralRe: Recursive Common Table Expressions Pin
CHill6021-Feb-16 22:19
mveCHill6021-Feb-16 22:19 
GeneralRe: Recursive Common Table Expressions Pin
Jörgen Andersson21-Feb-16 22:30
professionalJörgen Andersson21-Feb-16 22:30 
GeneralRe: Recursive Common Table Expressions Pin
Amr.Mohammad8722-Feb-16 10:28
Amr.Mohammad8722-Feb-16 10:28 
QuestionEntity Pin
mohsen47319-Feb-16 21:03
mohsen47319-Feb-16 21:03 
QuestionSQLPrepare error: [Microsoft][ODBC Microsoft Access Driver] Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'. Pin
Member 1233569519-Feb-16 17:48
Member 1233569519-Feb-16 17:48 
Questionhow to set/retrieve data directory and index directory of a table of mysql database Pin
CanadaProgrammer16-Feb-16 23:37
CanadaProgrammer16-Feb-16 23:37 
AnswerRe: how to set/retrieve data directory and index directory of a table of mysql database Pin
Richard MacCutchan16-Feb-16 23:54
mveRichard MacCutchan16-Feb-16 23:54 
QuestionHow to setup a local database for my application Pin
Theo Buys16-Feb-16 22:55
Theo Buys16-Feb-16 22:55 
AnswerRe: How to setup a local database for my application Pin
Richard MacCutchan16-Feb-16 23:52
mveRichard MacCutchan16-Feb-16 23:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.