Solved

how to the hirarchial data using sql query.

Posted on 2010-09-20
7
557 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

23 Experts available now in Live!

Get 1:1 Help Now