Feb 9, 2012 1:27 PM
linked list query
-
Like (0)
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]
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.
I know Java and Visual Basic and Perl - all I have for a starting point for procedures is this - it runs on my system. I don't know how to call it - where is the guide to the produre keywords functions etc.? How would you use the procedure here - how would I call it from a 'regular' SQL expression? Thanks
CREATE OR REPLACE PROCEDURE DimTime(Timestamp,Timestamp) RETURNS INT4 LANGUAGE NZPLSQL AS
BEGIN_PROC
DECLARE
starting_dt ALIAS for $1;
ending_dt ALIAS for $2;
diff int;
BEGIN
diff:=to_number(sum(ending_dt,starting_dt));
RETURNS diff;
END;
END_PROC;
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

