Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Billing of Materials query Expand / Collapse
Author
Message
Posted Wednesday, September 19, 2012 2:29 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, December 13, 2012 6:46 AM
Points: 72, Visits: 224
Hi there,

I'd be grateful if someone could help:)

I have a BoM issue.

The table below shows the components, A's being the highest level component B's are child components and C child components of B.. etc.

The table has two columns Parent and Child. Each parent can have many components and there can be up to 15 levels. (to keep it simple, I have given 4 levels)
Parent   Child
A1 B1
A1 B2
A1 B3
B1 C1
B1 C2
B1 C3
C1 D1
C1 D2
D1 E1
D1 E2

A2 B4
A2 B5
B5 C4

In this table A1, is related to B1, B2, B3 and indirectly related to all components within the top ten rows. It then goes to A2, to make this simple, none of the rows and indirect rows are related to the top ten rows.

What I want to attempt to produce is query with a Parent (A) parameter. When selected should look like this:

select Material from BoM where parent = 'A1' (purely just an example select for brevity)
Result
B1
B2
B3
C1
C2
C3
D1
D2
E1
E2

This is basically a list of all the components related directly or indirectly to A1.

The reason to do this is to list Quantities and purchase costs which I'll add later. As long as I can create this column of related components I'll be able to work out the rest.

Does anyone have a good solution for this?

Any help is very much appreciated.

Thanks
Post #1361621
Posted Wednesday, September 19, 2012 2:41 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 6:41 PM
Points: 11,648, Visits: 27,760
ok, i think this recursive CTE generates allt eh sub parts you are looking for:
DECLARE @Table  Table(
Parent varchar(2),
Child varchar(2) )
INSERT @Table
SELECT 'A1','B1' UNION ALL
SELECT 'A1','B2' UNION ALL
SELECT 'A1','B3' UNION ALL
SELECT 'B1','C1' UNION ALL
SELECT 'B1','C2' UNION ALL
SELECT 'B1','C3' UNION ALL
SELECT 'C1','D1' UNION ALL
SELECT 'C1','D2' UNION ALL
SELECT 'D1','E1' UNION ALL
SELECT 'D1','E2' UNION ALL
SELECT 'A2','B4' UNION ALL
SELECT 'A2','B5' UNION ALL
SELECT 'B5','C4';

WITH Dependencies AS(
SELECT Parent, Child
FROM @Table
WHERE Parent = 'A1'
UNION ALL
SELECT t.Parent, t.Child
FROM @Table t
JOIN Dependencies d ON t.Parent = d.Child
)
SELECT Child
FROM Dependencies



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1361626
Posted Wednesday, September 19, 2012 2:51 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, December 13, 2012 6:46 AM
Points: 72, Visits: 224
Thanks for responding Lowell,

The only issue with this technique is that there are literally 10s of thousands of parts so I'm not sure if this technique would work; I wont be able to hard code the values for this reason.
Post #1361629
Posted Wednesday, September 19, 2012 2:56 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 6:41 PM
Points: 11,648, Visits: 27,760
cidr (9/19/2012)
Thanks for responding Lowell,

The only issue with this technique is that there are literally 10s of thousands of parts so I'm not sure if this technique would work; I wont be able to hard code the values for this reason.


because you did not provide any table names, i had to generate the data based on what you posted.

by obfuscating everything to A1,A2 parent/Child, you lost the ability to get an answer you can translate over easily.


if you substitute 4 things in the example i provided, it will work with your data:
@Table <--> change to the real table name
Parent <-->real column name
child <--> real column name
WHERE Parent = 'A1' <-->change 'A1' to the real value
WITH Dependencies AS(
SELECT Parent, Child
FROM @Table
WHERE Parent = 'CAR'
UNION ALL
SELECT t.Parent, t.Child
FROM @Table t
JOIN Dependencies d ON t.Parent = d.Child
)
SELECT Parent,Child
FROM Dependencies



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1361630
Posted Wednesday, September 19, 2012 3:05 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:37 PM
Points: 21,627, Visits: 27,480
Lowell (9/19/2012)
cidr (9/19/2012)
Thanks for responding Lowell,

The only issue with this technique is that there are literally 10s of thousands of parts so I'm not sure if this technique would work; I wont be able to hard code the values for this reason.


because you did not provide any table names, i had to generate the data based on what you posted.

by obfuscating everything to A1,A2 parent/Child, you lost the ability to get an answer you can translate over easily.


if you substitute 4 things in the example i provided, it will work with your data:
@Table <--> change to the real table name
Parent <-->real column name
child <--> real column name
WHERE Parent = 'A1' <-->change 'A1' to the real value
WITH Dependencies AS(
SELECT Parent, Child
FROM @Table
WHERE Parent = 'CAR'
UNION ALL
SELECT t.Parent, t.Child
FROM @Table t
JOIN Dependencies d ON t.Parent = d.Child
)
SELECT Parent,Child
FROM Dependencies



And I thought there was no spoon.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1361637
Posted Wednesday, September 19, 2012 3:09 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, December 13, 2012 6:46 AM
Points: 72, Visits: 224

Lowell 2012-09-12

by obfuscating everything to A1,A2 parent/Child, you lost the ability to get an answer you can translate over easily.


Ah, I'm sorry Lowell, my bad, I've been zoned out all day I didn't realise that the values weren't hard coded and that the code was creating a table variable.

Sorry about that, you're answer looks good.

Thanks so much
Post #1361641
Posted Wednesday, September 19, 2012 3:09 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 6:41 PM
Points: 11,648, Visits: 27,760
Lynn Pettis (9/19/2012)

And I thought there was no spoon.

@67 posts, i didn't think this was homework for the OP, and i got all excited about making a recursive CTE work before someone else beat me to to it.

spoon fed answers, yeah, guilty as charged.


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1361642
Posted Wednesday, September 19, 2012 3:34 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, December 13, 2012 6:46 AM
Points: 72, Visits: 224

lowell 2012-09-12
@67 posts, i didn't think this was homework for the OP, and i got all excited about making a recursive CTE work before someone else beat me to to it.


Haha, I'm sorry but I do not know what this means; what's an OP?
Post #1361663
Posted Wednesday, September 19, 2012 3:35 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 6:41 PM
Points: 11,648, Visits: 27,760
cidr (9/19/2012)

lowell 2012-09-12
@67 posts, i didn't think this was homework for the OP, and i got all excited about making a recursive CTE work before someone else beat me to to it.


Haha, I'm sorry but I do not know what this means; what's an OP?

lol just shorthand for "Original Poster".


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1361664
Posted Wednesday, September 19, 2012 3:42 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, December 13, 2012 6:46 AM
Points: 72, Visits: 224
Lowell 2012-09-12

lol just shorthand for "Original Poster".


Er, I'll take that as a compliment.

I really have been out of touch for the last year
Post #1361671
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse