| Author |
Topic |
|
Ralex
Starting Member
2 Posts |
Posted - 2011-04-27 : 13:54:48
|
| I have a table with Id, Parent, Name, CodeI want to display my data like this1--1.1----1.1.1----1.1.2--1.223--3.1--3.2----3.2.1etc..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 wayCan 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 CTEORDER BY OrderingId; |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
|
|
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', nullInsert Into @t Select 3, 10, 'AA', nullInsert Into @t Select 4, 3, 'AAA', nullInsert Into @t Select 6, 10, 'AB', nullInsert Into @t Select 1, null, 'B', nullInsert Into @t Select 8, null, 'C', nullInsert Into @t Select 7, 8, 'CA', nullInsert Into @t Select 5, 8, 'CB', nullInsert 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 tieredOrder By ltrim(Display)Corey I Has Returned!! |
 |
|
|
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? |
 |
|
|
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!! |
 |
|
|
Ralex
Starting Member
2 Posts |
Posted - 2011-04-27 : 17:42:23
|
| Yeah! Sunitaveck solution work perfectThanks a lot |
 |
|
|
|
|
|