Solved

how to the hirarchial data using sql query.

Posted on 2010-09-20
7
564 Views
Last Modified: 2012-05-10
My table called employee contains the following data.
empid  empname jobname managerid
1        ravi   se       3

2        kumar  SSe      3

3        xyz    TeamLead 4

4        ppp    ProjectLead 5

5        MMM    Manager 6

6        ccc     CEo

7        ram     Fresher 2        
   




i need the output as follwors.

The input id mgrid.
 
i need the output in the hirarichael manner.
manager
projectlead
teamlead
sse
se
fresher
0
Comment
Question by:mu_ravi1
  • 3
  • 3
7 Comments
 
LVL 5

Expert Comment

by:Priya Perumpilavil
ID: 33722587
select * from table1 order by managerid desc

0
 

Author Comment

by:mu_ravi1
ID: 33723484
Hi Priya

i need to show the Hirarichal levels to top to bottom.
Above u have mentioned a simple query.
my thought of applying collase on the query to the level to level data in the output
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 33723659
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:mu_ravi1
ID: 33741043
Hi Emoreau,

Could please give me the code or qurey for my requirement.
it U do that it would be highly appreciable and it is a great help for me
Ravi
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 250 total points
ID: 33743129
use this:

declare @employee table 
  ( 
      empid int  Primary key NOT NULL , 
      managerid int, 
      empname nvarchar(100), 
      jobname nvarchar(100)
   )

insert into @employee( empid, managerid, empname, jobname )

select 1, 3, 'ravi', 'se'
union
select 2, 3, 'kumar', 'SSe'
union
select 3, 4, 'xyz', 'TeamLead'
union
select 4, 5, 'ppp', 'ProjectLead'
union
select 5, 6, 'MMM', 'Manager'
union
select 6, null, 'ccc', 'CEo'
union
select 7, 2, 'ram', 'Fresher'



SELECT 'original data', * FROM @employee;

with RecursionCTE (empID,managerid, empname, jobname) 
 as
 (
   select empid,managerid,empname, jobname 
      from @employee
      where managerid is null
   union all
   select R1.empid,
          R1.managerid,
          R1.empname,
          R1.jobname
      from @employee as R1
     join RecursionCTE as R2 on R1.managerid = R2.empid
  )

select 'new data', * from RecursionCTE 

Open in new window

0
 

Author Closing Comment

by:mu_ravi1
ID: 33744626
Thanks for ur help and support
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 33744667
A "B" grade for such a specific code!
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel conversion issue with Sql server 14 54
Help Required 2 33
domain login has permission in database, but gets error 3 16
Sql query with where clause 2 13
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question