Link to home
Start Free TrialLog in
Avatar of Omega002
Omega002Flag for Afghanistan

asked on

Display Hierarchy In Column Format

Greetings,

I am using this hierarchy function listed below and query to display my hierarchy in this table called ticket_category_tmp.
 for sql server 2000:
create function hirearchy(@id as int) returns varchar(1000)
as
begin
declare @tree as varchar(1000)
set @tree = ''
if (parent_cat_code <> 0 and  parent_cat_code is not  null )

select @tree = dbo.hirearchy(parent_Cat_code) +'>' + description from ticket_category_tmp where id = @id

return @tree
end
go  

SELECT     ticket_category.id, dbo.hirearchy(ticket_category.id)
FROM         ticket_category
ORDER BY dbo.hirearchy(ticket_category.id)

The Results of this query display like this:

ID         Tree
===  =================
7       Admin>
15     Admin>HardWare
16     Admin> HardWare>MBOARD
17     Admin>NETWORK
18     Admin>NETWORK>OUTAGE
19     Admin>NETWORK>OUTAGE>VPN

I would like to have this display by columns instead of rows. Any solutions for this? The first colum would be the root or parent then the next column would be level 1, level 2, level 3, etc......

Avatar of Omega002
Omega002
Flag of Afghanistan image

ASKER

Also could you do this using cases?
Avatar of Racim BOUDJAKDJI
SQL 2005 does use the PIVOT function to do that...OnSQL 2000 you will probably have to do some scripting for that...
Can you show me how you would do it?
Can you show me how you would use it in SQL 2005 using the pivot table?
Check the following link for more info...

http://msdn2.microsoft.com/en-us/library/ms177410.aspx

Hope this helps...
The example from the link is count number values so how would I apply this to displaying hierarchy data?
SELECT     ticket_category.id, dbo.hirearchy(ticket_category.id)
FROM         ticket_category
ORDER BY dbo.hirearchy(ticket_category.id)

The Results of this query display like this:

ID         Tree
===  =================
7       Admin>
15     Admin>HardWare
16     Admin> HardWare>MBOARD
17     Admin>NETWORK
18     Admin>NETWORK>OUTAGE
19     Admin>NETWORK>OUTAGE>VPN

This is what I am looking for:
ID    Level 1      Level 2      Level 3
=     ======  =====    ======
7       Admin      HardWare MBOARD
 Can you show me how you would approach this?
ASKER CERTIFIED SOLUTION
Avatar of imran_fast
imran_fast

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial