-1

I want to write a SQL Server SELECT query to display data in hierarchical in tabular format.

Like: employee reports to X manager, then X manager reports to XX manager, and then XX manager reports to XXX manager - for each line of record.

Table data:

Input data format

Output format:

output required format

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3530807
  • 19
  • 1
  • 4
  • Do you want pure SQL or is some programming language involved? – Niklaus May 22 '17 at 11:43
  • @Niklaus- pure sql – user3530807 May 22 '17 at 11:56
  • Depending on your input data this might be solveable with a recursice CTE... Please do not poste pictures! Please tag with the actual RDBMS (product and version) and please read [How to ask a good SQL question](http://meta.stackoverflow.com/questions/271055/tips-for-asking-a-good-structured-query-language-sql-question/271056) and [How to create a MCVE](http://stackoverflow.com/help/mcve) – Shnugo May 22 '17 at 13:08

1 Answers1

1

Simple, join 10 virtual tables together

Select person, Person1, Person2, ...
from people
left join
    (
    select person as Person1, Manager as Manager2 from People
    ) as People1
        on people.Manager = Person1
left join
    (
    select person as Person2, Manager as Manager3 from People
    ) as People2
        on Manager2 = Person2
....
JeffUK
  • 4,107
  • 2
  • 20
  • 34