Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 25th, 2003, 08:12 AM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default Hierarchies

Hello everybody
Here is my problem
I have two tables.
First is Products and second is ProductsPart.
Products:
CREATE TABLE [Products]
(
[ID] [char] (16),
[NAME] [char] (40)
)
ProductsPart:
CREATE TABLE [ProductsPart]
(
[ID] [char] (16),
[IDPOD] [char] (16),
[QUANTITY] [smallint]
)
All products are in the products table.
Some of products are made of the other products which are also in the products table. From which products the product is made you know with ProductsPart table because IDPOD field shows product from which is made product which ID is ID.

Let's say that Product1 is made from Product2 and Product3
If I want to see from which product Product1 is made I will write this query:

SELECT P.ID, P.NAME
FROM Products P JOIN ProductsPart PP
ON P.ID = PP.IDPOD
WHERE PP.ID = 'Product1'

I will get this results:
ID2 Product2
ID3 Product3


But what if the Product3 is made of the others products(Product4, Product5) and so on...(Product4 = Product6 + Product7)...
How can I make query get this results?
ID2 Product2
ID3 Product3
ID4 Product4
ID6 Product6
ID7 Product7
ID5 Product5

Thanks very much
Alex
 
Old July 26th, 2003, 06:39 AM
Authorized User
 
Join Date: May 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Select * from Products where iD in
(Select ID from Products where ID in (
Select DPOD
From ProductsPart
Where ID = 8
)
union
Select ID from Products
where ID in(
select DPOD from ProductsPart
where ID
in
(Select DPOD
From ProductsPart P
Where P.ID = 8)
))

This will solve your Problem


Quote:
quote:Originally posted by acko
 Hello everybody
Here is my problem
I have two tables.
First is Products and second is ProductsPart.
Products:
CREATE TABLE [Products]
(
[ID] [char] (16),
[NAME] [char] (40)
)
ProductsPart:
CREATE TABLE [ProductsPart]
(
[ID] [char] (16),
[IDPOD] [char] (16),
[QUANTITY] [smallint]
)
All products are in the products table.
Some of products are made of the other products which are also in the products table. From which products the product is made you know with ProductsPart table because IDPOD field shows product from which is made product which ID is ID.

Let's say that Product1 is made from Product2 and Product3
If I want to see from which product Product1 is made I will write this query:

SELECT P.ID, P.NAME
FROM Products P JOIN ProductsPart PP
ON P.ID = PP.IDPOD
WHERE PP.ID = 'Product1'

I will get this results:
ID2 Product2
ID3 Product3


But what if the Product3 is made of the others products(Product4, Product5) and so on...(Product4 = Product6 + Product7)...
How can I make query get this results?
ID2 Product2
ID3 Product3
ID4 Product4
ID6 Product6
ID7 Product7
ID5 Product5

Thanks very much
Alex
Deepesh Jain
VB,VBA & .NET Specialist
Wiley Support Team
 
Old July 26th, 2003, 09:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

There really isn't a good way to process a tree like this, assuming an arbitrary depth, without using some form of recursion, and SQL doesn't "do" recursion.

If your tree has a known fixed depth limit, you can construct a query using outer joins or subqueries to "flatten" out the hierarchy.

There are other structures which will allow you to define and traverse a tree of arbitrary complexity, but that subject is quite complex and really beyond the scope of any discussion here. I suggest you obtain the book "SQL for Smarties" by Joe Celko - he devotes an entire chapter to the problem of defining and processing trees in SQL - in fact I understand he is soon coming out with an entire book on the subject.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dimension Hierarchies Not Working orangepower SQL Server 2005 1 December 14th, 2006 03:41 PM
creating hierarchies juergen XSLT 0 September 1st, 2003 04:02 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.