Solved

how to the hirarchial data using sql query.

Posted on 2010-09-20
7
560 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 69

Expert Comment

by:Éric Moreau
ID: 33723659
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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 69

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 69

Expert Comment

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

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query 2 43
MS SQL - Rotating Values in SQL 9 53
Generate Weekly Schedule 15 17
create insert script based on records in a table 4 15
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

895 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now