Solved

From parent-child model to nested intervals in SQL Server

Posted on 2007-03-30
10
357 Views
Last Modified: 2008-02-01
I have a table like this:

Create Table Category
(
CatID   Int Null,
CatName Varchar(20),
ParentID Int,
Lft Int,
Rgt Int)


Insert Into Category Values(1, 'Category 1', Null, Null, Null);
Insert Into Category Values(2, 'Category 2', 1, Null, Null);
Insert Into Category Values(3, 'Category 3', 1, Null, Null);
Insert Into Category Values(4, 'Category 4', 3, Null, Null);
Insert Into Category Values(5, 'Category 5', 3, Null, Null);
Insert Into Category Values(6, 'Category 6', 2, Null, Null);
Insert Into Category Values(7, 'Category 7', 2, Null, Null);

I want to fill the Lft and Rgt columns with values needed to handle the hierarchy with the nested intervals model.
I have right here, a Joe Celko's book solely dedicated to hierarchies and guess what, it doesn't explain exactly this.
0
Comment
Question by:fischermx
  • 5
  • 5
10 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 18827662
Insert Into Category Values(1, 'Category 1', Null, 1, 14);
Insert Into Category Values(2, 'Category 2', 1, 2, 7);
Insert Into Category Values(3, 'Category 3', 1, 8, 13);
Insert Into Category Values(4, 'Category 4', 3, 9, 10);
Insert Into Category Values(5, 'Category 5', 3, 11, 12);
Insert Into Category Values(6, 'Category 6', 2, 3, 4);
Insert Into Category Values(7, 'Category 7', 2, 5, 6);

Once lft and rgt are populated, you no longer need parentid column
0
 
LVL 1

Author Comment

by:fischermx
ID: 18827668
But the thing is, I have 12,000 rows in this table. Clearly you did this by hand/eye.

I'd like to know the <b>procedure</b> to generate the lft and rgt columns for these 12,000 rows.

0
 
LVL 1

Author Comment

by:fischermx
ID: 18827669
BTW, when in a given hierarchy table you just have the Parent ID column to help you represent the tree, what's the name of that model?
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 42

Accepted Solution

by:
dqmq earned 500 total points
ID: 18827693
Sure, I did it by hand/eye.   Here's Joe's procedure for converting from from the adjacency list to the nested set.  Perhaps you can adapt it to your table:

-- Tree holds the adjacency model
CREATE TABLE Tree
(emp CHAR(10) NOT NULL,   --CatID in your case
 boss CHAR(10));                  --ParentID in your case

INSERT INTO Tree
SELECT emp, boss FROM Personnel;    --From Category in your Case

-- Stack starts empty, will holds the nested set model
CREATE TABLE Stack
(stack_top INTEGER NOT NULL,    
 emp CHAR(10) NOT NULL,
 lft INTEGER,
 rgt INTEGER);

BEGIN ATOMIC
DECLARE counter INTEGER;
DECLARE max_counter INTEGER;
DECLARE current_top INTEGER;

SET counter = 2;
SET max_counter = 2 * (SELECT COUNT(*) FROM Tree);
SET current_top = 1;

INSERT INTO Stack
SELECT 1, emp, 1, NULL
  FROM Tree
 WHERE boss IS NULL;

DELETE FROM Tree
 WHERE boss IS NULL;

WHILE counter <= (max_counter - 2)
LOOP IF EXISTS (SELECT *
                   FROM Stack AS S1, Tree AS T1
                  WHERE S1.emp = T1.boss
                    AND S1.stack_top = current_top)
     THEN
     BEGIN -- push when top has subordinates, set lft value
       INSERT INTO Stack
       SELECT (current_top + 1), MIN(T1.emp), counter, NULL
         FROM Stack AS S1, Tree AS T1
        WHERE S1.emp = T1.boss
          AND S1.stack_top = current_top;

        DELETE FROM Tree
         WHERE emp = (SELECT emp
                        FROM Stack
                       WHERE stack_top = current_top + 1);

        SET counter = counter + 1;
        SET current_top = current_top + 1;
     END
     ELSE
     BEGIN  -- pop the stack and set rgt value
       UPDATE Stack
          SET rgt = counter,
              stack_top = -stack_top -- pops the stack
        WHERE stack_top = current_top
       SET counter = counter + 1;
       SET current_top = current_top - 1;
     END IF;
 END LOOP;
END;





0
 
LVL 42

Expert Comment

by:dqmq
ID: 18827740
Celko refers parent relationship design as the "adjaceny model".  Not a very intuitive name IMHO.  
0
 
LVL 1

Author Comment

by:fischermx
ID: 18827795
What SQL dialect is that ?
0
 
LVL 42

Expert Comment

by:dqmq
ID: 18831003
I don't know.  But I only see a couple  statements that won't port to SQL SERVER:
BEGIN ATOMIC
WHILE... LOOP...  END LOOP
0
 
LVL 1

Author Comment

by:fischermx
ID: 18831141
... and all the variables declaration and use, they're missing the "@".

Anyway, I got it working. Thanks !
0
 
LVL 42

Expert Comment

by:dqmq
ID: 18832333
Whoa...I totally overlooked the "@".   Glad (Joe and) I could be of some help.  
0
 
LVL 1

Author Comment

by:fischermx
ID: 18832496
He is using standard SQL-92, that's why he got that syntax that seems not to match exactly any known DBMS.

0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question