Solved

From parent-child model to nested intervals in SQL Server

Posted on 2007-03-30
10
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

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…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

752 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