Self-Referencing Query
From: Aaron Weiker (aaron_at_sqlprogrammer.org)
Date: 02/01/05
- Next message: Alejandro Mesa: "RE: How can this be ordered/grouped?"
- Previous message: Alejandro Mesa: "Re: Improving a query: multiple MIN() aggregate columns"
- Next in thread: Alejandro Mesa: "RE: Self-Referencing Query"
- Reply: Alejandro Mesa: "RE: Self-Referencing Query"
- Reply: oj: "Re: Self-Referencing Query"
- Reply: David D Webb: "Re: Self-Referencing Query"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 31 Jan 2005 16:24:34 -0800
Problem: I want to create a menu tree by looking up the table heirarchy from
a single table. For instance I have one row that points to a previous to
identify that it belongs in that "folder". Think of it as a file system type
approach. Ideally I want to represent this as an XML file as I already wrote
a XSLT file that parses it and creates a nice tree on a web page. For an
example check out the webpage at http://stats.weiker.org/. It uses this XML
File to pull the content from. http://stats.weiker.org/links.xml
Here is the DDL for the table.
CREATE TABLE dbo.Menus
(
[Id] int not null identity(1,1),
[ParentId] int,
[Name] varchar(128) not null,
[Order] int not null
CONSTRAINT DF_Menus_Order DEFAULT(0),
[Url] varchar(1024),
[CreateDate] smalldatetime not null
CONSTRAINT DF_Menus_CreateDate DEFAULT(GETDATE()),
CONSTRAINT PK_Menus PRIMARY KEY CLUSTERED ([Id]),
CONSTRAINT UK_Menus UNIQUE ([Name], [ParentId]),
CONSTRAINT FK_Menus_Menus
FOREIGN KEY ([ParentId])
REFERENCES dbo.Menus([Id])
)
The problem I keep running into creating the heirarchy. For example
Folder 1
--> Item A
--> Item B
--> Item C
-->Folder 2
-->--> Item A
-->--> Item F
Folder 999
--> Item Z
--> Item Y
The only solution I currently see is to look through each item and then retrieve
the items that belong to that and perform recursively. Whenever I see this
pattern I immediately see my CPU soaring and I break into a cold sweat. Maybe
not that bad but you get the picture.
So have I over normalized into this nice little design that I really want
to work but in reality it is going to cost too much to do?
Or am I just crazy and should just stick to using a pure XML file approach?
Right now I'm thinking this might be much simpler, but that would require
accepting defeat.
-- Aaron Weiker http://aaronweiker.com/ http://sqlprogrammer.org/
- Next message: Alejandro Mesa: "RE: How can this be ordered/grouped?"
- Previous message: Alejandro Mesa: "Re: Improving a query: multiple MIN() aggregate columns"
- Next in thread: Alejandro Mesa: "RE: Self-Referencing Query"
- Reply: Alejandro Mesa: "RE: Self-Referencing Query"
- Reply: oj: "Re: Self-Referencing Query"
- Reply: David D Webb: "Re: Self-Referencing Query"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|