0

i have the following simplified information in a table called suites which unfortunately i cannot control

id  title            parentsuiteid
1   test             NULL
4   pay1             3
5   pay2             3
3   Bill Payments    2
14  Vouchers         2
15  Mini             2
2   Site Tests       NULL

I would like to have a horizontal representation to use for reporting such as

test
Site tests   Bill Payments  pay1
Site tests   Bill Payments  pay2
Site tests   Vouchers   
Site tests   Mini   

i was testing with the join

select a.id,a.title,b.title,a.parentsuiteid from #temp2 a
left outer join #temp2 b
on a.id = b.parentsuiteid

id  title           title       parentsuiteid
1   test            NULL            NULL
4   pay1            NULL            3
5   pay2            NULL            3
3   Bill Payments   pay1            2
3   Bill Payments   pay2            2
14  Vouchers        NULL            2
15  Mini            NULL            2
2   Site Tests      Bill Payments   NULL
2   Site Tests      Vouchers        NULL
2   Site Tests      Mini            NULL 

This works with two levels fine but i cannot predict how many levels in future there will be and it seems to get complicated with more than two

How do i get my output to look like the horizontal representation for +- 5 levels ?

user2168435
  • 732
  • 2
  • 10
  • 27
  • What exactly is your question? – maryjane Jan 06 '15 at 13:51
  • If you have N levels then you need a recursive SQL query, also known as a CTE. See http://stackoverflow.com/questions/1757260/simplest-way-to-do-a-recursive-self-join-in-sql-server – jhilden Jan 06 '15 at 14:02

2 Answers2

4

Try this:

SQL Fiddle

MS SQL Server 2008 Schema Setup:

CREATE TABLE temp
(
  ID int,
  title VARCHAR(50),
  parentsuiteid int null
)

INSERT INTO temp
VALUES
(1, 'test', NULL),
(4, 'pay1',3),
(5, 'pay2', 3),
(3, 'Bill Payments', 2),
(14, 'Vouchers', 2),
(15, 'Mini', 2),
(2, 'Site Tests', NULL)

Query 1:

;WITH recurs
AS
(
    SELECT ID, title, parentsuiteid, 0 as level
    FROM Temp
    WHERE parentsuiteid IS NULL
    UNION ALL
    SELECT t1.ID, CAST(t2.title + ' ' + t1.title as VARCHAR(50)), t1.parentsuiteid, t2.level + 1 
    FROM temp t1
    INNER JOIN recurs t2
        ON t1.parentsuiteid = t2.ID
)
SELECT title
FROM Recurs r1
WHERE NOT EXISTS (SELECT * from recurs r2 WHERE r2.parentsuiteid = r1.Id )

Results:

|                         TITLE |
|-------------------------------|
|                          test |
|           Site Tests Vouchers |
|               Site Tests Mini |
| Site Tests Bill Payments pay1 |
| Site Tests Bill Payments pay2 |
Steve Ford
  • 7,433
  • 19
  • 40
-2

You can use Recursive CTE's :

WITH EmpsCTE
AS (
 SELECT empid
    ,mgrid
    ,firstname
    ,lastname
    ,0 AS distance
FROM HR.Employees
WHERE empid = 9

UNION ALL

SELECT M.empid
    ,M.mgrid
    ,M.firstname
    ,M.lastname
    ,S.distance + 1 AS distance
FROM EmpsCTE AS S
INNER JOIN HR.Employees AS M ON S.mgrid = M.empid
)
SELECT empid
,mgrid
,firstname
,lastname
,distance
FROM EmpsCTE;

empid       mgrid       firstname  lastname             distance 
----------- ----------- ---------- -------------------- ----------- 
9           5           Zoya       Dolgopyatova         0 
5           2           Sven       Buck                 1 
2           1           Don        Funk                 2 
1           NULL        Sara       Davis                3
  • 3
    This SQL doesn't reflect the question. if you're going to copy and paste code, at least make it relevant to the question. – Tanner Jan 06 '15 at 14:05
  • I don't want to provide him the exact answer. That is the reason i have provided an example so that he will work on it and modify according to his requirements..!! – Kishore Kumar Jan 06 '15 at 14:07
  • 1
    If you don't wish to provide an 'exact answer', you should provide a detailed explanation. This is *less helpful* than an explanation. If the OP doesn't understand what to do, a bare example without explanation isn't likely to help. Another option is not to post an answer in the first place. – Andrew Barber Jan 06 '15 at 14:24