Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Hierarchical display with CTEs

Author  Topic 

Ralex
Starting Member

2 Posts

Posted - 2011-04-27 : 13:54:48
I have a table with Id, Parent, Name, Code

I want to display my data like this

1
--1.1
----1.1.1
----1.1.2
--1.2
2
3
--3.1
--3.2
----3.2.1

etc..

I have been trying to find out how to make it work with a Recursive Queries Using Common Table Expressions but i cant find the way

Can anyone help me?

Thanks

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-04-27 : 15:15:41
One way of doing it - if it works for you, I wrote it. If it doesn't work, I have no idea who hacked into my account and posted this!!
CREATE TABLE dbo.Test
(
Id INT NOT NULL PRIMARY KEY CLUSTERED,
Parent INT,
name VARCHAR(32),
code varchar(1024)
);

insert into dbo.Test values (10,null, 'GreatGrandFather',null);
insert into dbo.Test values (2,10, 'GrandFather1',null);
insert into dbo.Test values (1,10, 'GrandFather2',null);
insert into dbo.Test values (3,10, 'GrandMother1',null);
insert into dbo.Test values (4,3, 'Mother1',null);
insert into dbo.Test values (5,3, 'Father1',null);
insert into dbo.Test values (6,5, 'Child1',null);

WITH CTE AS
(
SELECT
*,
CAST(id AS VARBINARY(MAX)) AS OrderingId,
CAST(Id AS VARCHAR(MAX)) AS AnotherOrderingIdJustForTheFunOfIt
FROM
dbo.Test
WHERE
Parent IS NULL
UNION ALL
SELECT
t.*,
c.OrderingId + CAST(t.id AS VARBINARY(MAX)),
c.AnotherOrderingIdJustForTheFunOfIt + '.' + CAST(t.Id AS VARCHAR(MAX))
FROM
dbo.Test t
INNER JOIN CTE c
ON c.Id = t.Parent
)
SELECT
*
FROM
CTE
ORDER BY
OrderingId;
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-04-27 : 15:19:09
BTW, if you are on SQL 2008, check out hierarchyid

http://msdn.microsoft.com/en-us/magazine/cc794278.aspx
http://msdn.microsoft.com/en-us/library/bb677290.aspx
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-04-27 : 15:39:40
Here is another article that might help:
http://www.sqlservercentral.com/articles/T-SQL/72503/
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-04-27 : 15:49:09
Weee...


Declare @t Table (
id int,
parent int,
Name varchar(10),
Code varchar(10)
)

Insert Into @t Select 10, null, 'A', null
Insert Into @t Select 3, 10, 'AA', null
Insert Into @t Select 4, 3, 'AAA', null
Insert Into @t Select 6, 10, 'AB', null
Insert Into @t Select 1, null, 'B', null
Insert Into @t Select 8, null, 'C', null
Insert Into @t Select 7, 8, 'CA', null
Insert Into @t Select 5, 8, 'CB', null
Insert Into @t Select 9, 5, 'CBA', null


; with ranked (id, parent, name, code, r)
As (
Select
*,
r = Row_Number() Over(Partition By parent Order By name)
From @t
), tiered
As (
Select
A.*,
Display = convert(varchar(1000),r),
Level = 1
From ranked A
Where A.parent is null

Union All

Select
A.*,
Display = convert(varchar(1000),replicate(' ',B.Level) + B.Display + '.' + convert(varchar,A.r)),
Level = B.Level+1
From ranked A
Inner Join tiered B
On B.id = A.parent

)

Select *
From tiered
Order By ltrim(Display)



Corey

I Has Returned!!
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-04-27 : 16:06:27
quote:
Originally posted by Seventhnight

Weee...



Do you really make those noises and make faces as you are typing the responses?
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-04-27 : 17:01:22

Shore do.

quote:
Originally posted by sunitabeck

quote:
Originally posted by Seventhnight

Weee...



Do you really make those noises and make faces as you are typing the responses?



Corey

I Has Returned!!
Go to Top of Page

Ralex
Starting Member

2 Posts

Posted - 2011-04-27 : 17:42:23
Yeah! Sunitaveck solution work perfect

Thanks a lot
Go to Top of Page
   

- Advertisement -