Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
erwine
Yak Posting Veteran
69 Posts |
Posted - 2007-04-03 : 03:59:09
|
Hi all,I have these two tables:TABLE1:ProductNo. ComponentCH-001 A100 CH-001 B100CH-001 C100TABLE2:Parent UniqueNo. Order ItemCodeA100 1 1 AKA100 2 2 ALA100 3 3 AMA100 4 4 OOAK 5 1 AMAK 6 2 OOAK 7 3 UKB100 8 1 BSB100 9 2 BTB100 10 3 ALB100 11 4 OOC100 12 1 OOC100 13 2 KOD100 14 1 DPD100 15 2 OOwhich means that product CH-001 is made of components A100,B100 & C100. and the components are also made from sub-components. It is stored in two tables only.is there any way that I can write a SQL statement to have a list like:Product No. Component UniqueNo Order ItemCodeCH-001 A100 4 4 OOCH-001 AK 6 2 OO CH-001 B100 11 4 OOCH-001 C100 12 1 OONote: Notice that i only interested in all item OO used to produce CH-001 (that's why component D100 is not included) and the list is sorted in such a way that it will go from product to component and to sub-component and so on, then back to component.I dont want to use cursor to loop. Do you guys have any idea to write a join statement for this?Thanks for your kind attention.Cheers,erwine... sql is fun... |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-03 : 04:14:50
|
how many levels parent - child do you have ? Is there a max ? KH |
 |
|
erwine
Yak Posting Veteran
69 Posts |
Posted - 2007-04-03 : 04:17:19
|
Hi KHTan,Thanks for replying. Current situation: the maximum is only 3 levels. However, it is very possible to have more than 3 levels in the future. cheers,erwine... sql is fun... |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-03 : 04:40:02
|
[code]-- Prepare sample dataDECLARE @Table1 TABLE (ProductNo VARCHAR(6), Component VARCHAR(4))INSERT @Table1SELECT 'CH-001', 'A100' UNION ALLSELECT 'CH-001', 'B100' UNION ALLSELECT 'CH-001', 'C100'DECLARE @Table2 TABLE (Parent VARCHAR(4), UniqueNo INT, [Order] INT, ItemCode VARCHAR(2))INSERT @Table2SELECT 'A100', 1, 1, 'AK' UNION ALLSELECT 'A100', 2, 2, 'AL' UNION ALLSELECT 'A100', 3, 3, 'AM' UNION ALLSELECT 'A100', 4, 4, 'OO' UNION ALLSELECT 'AK', 5, 1, 'AM' UNION ALLSELECT 'AK', 6, 2, 'OO' UNION ALLSELECT 'AK', 7, 3, 'UK' UNION ALLSELECT 'B100', 8, 1, 'BS' UNION ALLSELECT 'B100', 9, 2, 'BT' UNION ALLSELECT 'B100', 10, 3, 'AL' UNION ALLSELECT 'B100', 11, 4, 'OO' UNION ALLSELECT 'C100', 12, 1, 'OO' UNION ALLSELECT 'C100', 13, 2, 'KO' UNION ALLSELECT 'D100', 14, 1, 'DP' UNION ALLSELECT 'D100', 15, 2, 'OO'-- Show the expected outputSELECT t1.ProductNo, t1.Component, t2.UniqueNo, t2.[Order], t2.ItemCodeFROM @Table1 AS t1LEFT JOIN @Table2 AS t2 ON t2.Parent = t1.ComponentWHERE t2.ItemCode = 'OO'UNION ALLSELECT t1.ProductNo, t2.ItemCode AS Component, t3.UniqueNo, t3.[Order], t3.ItemCodeFROM @Table1 AS t1LEFT JOIN @Table2 AS t2 ON t2.Parent = t1.ComponentLEFT JOIN @Table2 AS t3 ON t3.Parent = t2.ItemCodeWHERE t3.ItemCode = 'OO'UNION ALLSELECT t1.ProductNo, t3.ItemCode AS Component, t4.UniqueNo, t4.[Order], t4.ItemCodeFROM @Table1 AS t1LEFT JOIN @Table2 AS t2 ON t2.Parent = t1.ComponentLEFT JOIN @Table2 AS t3 ON t3.Parent = t2.ItemCodeLEFT JOIN @Table2 AS t4 ON t4.Parent = t3.ItemCodeWHERE t4.ItemCode = 'OO'UNION ALLSELECT t1.ProductNo, t4.ItemCode AS Component, t5.UniqueNo, t5.[Order], t5.ItemCodeFROM @Table1 AS t1LEFT JOIN @Table2 AS t2 ON t2.Parent = t1.ComponentLEFT JOIN @Table2 AS t3 ON t3.Parent = t2.ItemCodeLEFT JOIN @Table2 AS t4 ON t4.Parent = t3.ItemCodeLEFT JOIN @Table2 AS t5 ON t5.Parent = t4.ItemCodeWHERE t5.ItemCode = 'OO'ORDER BY 1, 2, 3, 4[/code]Peter LarssonHelsingborg, Sweden |
 |
|
erwine
Yak Posting Veteran
69 Posts |
Posted - 2007-04-03 : 23:16:10
|
Hi Peter,1st of all, thanks for replying.Am i right to say that your solution is only working for less than 4 levels? cheers,erwine... sql is fun... |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-03 : 23:30:29
|
quote: I dont want to use cursor to loop.
Well, This is the trade-off. KH |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-04 : 00:40:45
|
Just add a newUNION ALLSELECT t1.ProductNo, t5.ItemCode AS Component, t6.UniqueNo, t6.[Order], t6.ItemCodeFROM @Table1 AS t1LEFT JOIN @Table2 AS t2 ON t2.Parent = t1.ComponentLEFT JOIN @Table2 AS t3 ON t3.Parent = t2.ItemCodeLEFT JOIN @Table2 AS t4 ON t4.Parent = t3.ItemCodeLEFT JOIN @Table2 AS t5 ON t5.Parent = t4.ItemCodeLEFT JOIN @Table2 AS t6 ON t6.Parent = t5.ItemCodeWHERE t6.ItemCode = 'OO' Do you see the pattern?Peter LarssonHelsingborg, Sweden |
 |
|
erwine
Yak Posting Veteran
69 Posts |
Posted - 2007-04-04 : 02:59:46
|
Hi,Yeah..perhaps i need to get the max. level, using 3rd party codes to generate the query based on the level and then execute it. thanks guys.cheers,erwine... sql is fun... |
 |
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2007-04-04 : 10:28:07
|
quote: Originally posted by erwine I dont want to use cursor to loop. Do you guys have any idea to write a join statement for this?
Why not?!?!?When you place ridiculous and unnecessary restrictions on your requirements, you end up with ridiculous and unnecessary code such as this.e4 d5 xd5 Nf6 |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-04 : 11:24:40
|
You don't want a CURSOR to loop?But is it ok to loop, without a CURSOR?Peter LarssonHelsingborg, Sweden |
 |
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2007-04-04 : 13:32:18
|
Cursors are to be avoided, but not outlawed.Though in this case a cursor is not necessary because a simple WHILE loop is both sufficient and efficient.e4 d5 xd5 Nf6 |
 |
|
erwine
Yak Posting Veteran
69 Posts |
Posted - 2007-04-08 : 22:39:04
|
Hi guys,Thanks for your opinions.--------------------------------------------------------------------Though in this case a cursor is not necessary because a simple WHILE loop is both sufficient and efficient.--------------------------------------------------------------------Admittedly, im not a gud sql programmer. However, im still not convinced that a simple WHILE will do. Or perhaps your 'simple' is my 'complicated'. Mind to share some knowledge of how to use WHILE loop to produce efficient result?Thanks again for your kind attention.cheers,erwine... sql is fun... |
 |
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2007-04-09 : 10:40:55
|
http://sqlblindman.googlepages.com/returningchildrecordse4 d5 xd5 Nf6 |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|
|