Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > IBM DB2 > CTE CONVERSION ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 14 Topic 8881 of 9520
Post > Topic >>

CTE CONVERSION TO DB2

by "lenygold" <u41482@[EMAIL PROTECTED] > May 13, 2008 at 10:25 AM

Here is the input table:
    MPID     MGRID  EMPNAME    SALARY   
----------- ----------- -------------------------       ---------
         14           9 Didi                        1500.00
          1           - David                      10000.00
          2           1 Eitan                       7000.00
          3           1 Ina                         7500.00
          4           2 Seraph                      5000.00
          5           2 Jiru                        5500.00
          6           2 Steve                       4500.00
          7           3 Aaron                       5000.00
          8           5 Lilach                      3500.00
          9           7 Rita                        3000.00
         10           5 Sean                        3000.00
         11           7 Gabriel                     3000.00
         12           9 Emilia                      2000.00
         13           9 Michael                     2000.00

  14 record(s) selected. 

output table:

-- Output
empid	lvl	lft	rgt
———————	———————	
1	0	1	28
2	1	2	13
5	2	3	8
8	3	4	5
10	3	6	7
4	2	9	10
6	2	11	12
3	1	14	27
7	2	15	26
11	3	16	17
9	3	18	25
14	4	19	20
12	4	21	22
13	4	23	24


This  NON DB2 CTE  IS converting  adjancey set model to a nested set model

 with a left and right.
---------------------------------------------------------------------
-- CTE Code That Creates Nested Sets Relation****ps
---------------------------------------------------------------------
DECLARE @[EMAIL PROTECTED]
 AS INT;
SET @[EMAIL PROTECTED]
 = 1;
-- CTE with two numbers: 1 and 2
WITH TwoNumsCTE
AS
(
SELECT 1 AS n UNION ALL SELECT 2
),
-- CTE with two binary sort paths for each node:
-- One smaller than descendants sort paths
-- One greater than descendants sort paths
SortPathCTE
AS
(
SELECT empid, 0 AS lvl, n,
CAST(n AS VARBINARY(MAX)) AS sortpath
FROM dbo.Employees CROSS JOIN TwoNumsCTE
WHERE empid = @[EMAIL PROTECTED]
 ALL
SELECT C.empid, P.lvl + 1, TN.n,
P.sortpath + CAST(
ROW_NUMBER() OVER(PARTITION BY C.mgrid
-- *** determines order of siblings ***
ORDER BY C.empname, C.empid, TN.n)
AS BINARY(4))
FROM SortPathCTE AS P
JOIN dbo.Employees AS C
ON P.n = 1
AND C.mgrid = P.empid
CROSS JOIN TwoNumsCTE AS TN
),
-- CTE with Row Numbers Representing sortpath Order
SortCTE
AS
(
SELECT empid, lvl,
ROW_NUMBER() OVER(ORDER BY sortpath) AS sortval
FROM SortPathCTE
),
-- CTE with Left and Right Values Representing
-- Nested Sets Relation****ps
NestedSetsCTE
AS
(
SELECT empid, lvl, MIN(sortval) AS lft, MAX(sortval) AS rgt
FROM SortCTE
GROUP BY empid, lvl
)
SELECT * FROM NestedSetsCTE ORDER BY lft;

I am trying to convert it to DB2:

WITH T0(ROOT) AS
       (SELECT 1 FROM SYSIBM.SYSDUMMY1),
    TwoNumsCTE(N) AS
     (SELECT 1 FROM SYSIBM.SYSDUMMY1
     UNION ALL
      SELECT 2 FROM SYSIBM.SYSDUMMY1
      ORDER BY 1),
  SortPathCTE(empid,lvl,n,sortpath) AS
     (SELECT empid, 0, n,INTEGER(CAST(CHAR(n) AS varchar(300))) AS
sortpath
        FROM dbo_Employees,T0
         CROSS JOIN TwoNumsCTE
        WHERE empid = ROOT
  UNION ALL
    SELECT C.empid, P.lvl + 1, TN.n,
    P.sortpath + ROW_NUMBER() OVER(PARTITION BY C.mgrid
      ORDER BY C.empname, C.empid, TN.n)
      FROM SortPathCTE AS P
    JOIN dbo_Employees AS C
    ON P.n = 1
     AND C.mgrid = P.empid
      CROSS JOIN TwoNumsCTE AS TN)
SELECT * FROM SortPathCTE;

At this point i got an error:

sqlcode: -345
The fullselect of the recursive common table expressionmust be the UNION
of
two or more 
fullselects and cannot include column functions, GROUP BY clause, HAVING
clause, ORDER BY clause.
 Please help with conversion.
Thank's in advance. Leny G.

-- 
Message posted via DBMonster.com
http://www.dbmonster.com/Uwe/Forums.aspx/ibm-db2/200805/1
 




 14 Posts in Topic:
CTE CONVERSION TO DB2
"lenygold" <  2008-05-13 10:25:29 
Re: CTE CONVERSION TO DB2
Serge Rielau <srielau@  2008-05-13 06:45:26 
Re: CTE CONVERSION TO DB2
"lenygold via DBMons  2008-05-13 12:40:52 
Re: CTE CONVERSION TO DB2
Serge Rielau <srielau@  2008-05-13 09:45:28 
Re: CTE CONVERSION TO DB2
"lenygold via DBMons  2008-05-13 14:36:06 
Re: CTE CONVERSION TO DB2
Serge Rielau <srielau@  2008-05-13 14:26:42 
Re: CTE CONVERSION TO DB2
Lennart <Erik.Lennart.  2008-05-13 05:51:03 
Re: CTE CONVERSION TO DB2
Lennart <Erik.Lennart.  2008-05-13 07:17:17 
Re: CTE CONVERSION TO DB2
Serge Rielau <srielau@  2008-05-13 10:27:33 
Re: CTE CONVERSION TO DB2
Lennart <Erik.Lennart.  2008-05-13 08:40:48 
Re: CTE CONVERSION TO DB2
"Dave Hughes" &  2008-05-13 11:32:20 
Re: CTE CONVERSION TO DB2
Lennart <Erik.Lennart.  2008-05-13 10:41:40 
Re: CTE CONVERSION TO DB2
Serge Rielau <srielau@  2008-05-13 14:29:59 
Re: CTE CONVERSION TO DB2
Lennart <Erik.Lennart.  2008-05-13 12:24:54 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Dec 2 20:59:23 CST 2008.