Skip navigation
1226 Views 3 Replies Latest reply: Feb 12, 2012 2:28 AM by ulab RSS
Tom New Enzee 6 posts since
Oct 21, 2010
Currently Being Moderated

Feb 9, 2012 1:27 PM

linked list query

Hi -

 

Can't seem to do this - I know sql is not good at recursion         WITH keyword equivqlant? 

CREATE TABLE LinkedList(Id nvarchar(6) NOT NULL, ParentId nvarchar(6) NULL, SomeData nvarchar(50) NOT NULL,
sortorder nvarchar(1) NOT NULL);

 

insert into LinkedList(Id,ParentId,SomeData,sortorder) values ('24971',NULL,'Black',0);
insert into LinkedList(Id,ParentId,SomeData,sortorder) values ('38324','24971','Blue',1);
insert into LinkedList(Id,ParentId,SomeData,sortorder) values ('60088','60089','Red',3);
insert into LinkedList(Id,ParentId,SomeData,sortorder) values ('60089','38324','Orange',2);
insert into LinkedList(Id,ParentId,SomeData,sortorder) values ('61039','61497','Purple',5);
insert into LinkedList(Id,ParentId,SomeData,sortorder) values ('61497','60088','Green',4);
insert into LinkedList(Id,ParentId,SomeData,sortorder) values ('109397','109831','Brown',7);
insert into LinkedList(Id,ParentId,SomeData,sortorder) values ('109831','61039','Yellow',6);

 

select * from LinkedList

 

The only thing I find on Google is Oracle and Netezza syntax doesn't work

 

SELECT Id, ParentId, SomeData FROM (SELECT ll.*, level AS lvl FROM LinkedList ll
START WITH ParentID IS NULL CONNECT BY ParentId = PRIOR Id )
ORDER BY   lvl

 

 

or SQL server

 

WITH
SortedList (Id, ParentId, SomeData, Level) AS
(SELECT Id, ParentId, SomeData,
0 as Level FROM LinkedList WHERE ParentId IS NULL
UNION ALL
SELECT ll.Id, ll.ParentId, ll.SomeData, Level+1 as Level
FROM LinkedList ll INNER JOIN SortedList as s ON ll.ParentId = s.Id)
SELECT Id, ParentId, SomeData
FROM SortedList ORDER BY L

 

apparently to get right sort order you do this, but not I don't know what [level] is  ....

 

WITH cte (Id, ParentId, SomeData, [Level]) AS (SELECT Id, ParentId, SomeData, 0 FROM
LinkedList WHERE ParentId IS NULL     UNION ALL
SELECT ll.Id, ll.ParentId, ll.SomeData, cte.[Level] + 1
FROM LinkedList ll     INNER JOIN cte ON ll.ParentID = cte.ID )
SELECT * FROM cte ORDER BY [Level]

  • nickg Active Enzee 248 posts since
    Mar 16, 2011
    Currently Being Moderated
    1. Feb 9, 2012 1:37 PM (in response to Tom)
    Re: linked list query

    If you are trying to traverse the hierarchy, you need to write a procedure to do so.  There are ways to do this interatively (rather than recursively) in any SQL (including Netezza).  The output should be a table with the tree exploded so you don't need to repeatedly traverse the recursive structure it in any query that uses it.  An exploded tree structure allows you to traverse the tree using simple SQL - no recursion.

  • ulab New Enzee 62 posts since
    Jul 1, 2011
    Currently Being Moderated
    3. Feb 12, 2012 2:28 AM (in response to Tom)
    Re: linked list query

    you can call the procedure using simple select query...i didnot use it with perl but i did that with shell and with perl i guess it is similar..

     

    result=`nzsql -u < userid> -pw <password> -db <db name> -host <hostname>  -t -c "select <PROCEDURE NAME> "`

    echo $result

More Like This

  • Retrieving data ...

Bookmarked By (0)