| Author |
Topic |
|
DanG
Starting Member
10 Posts |
Posted - 2003-04-14 : 16:11:57
|
| Given the following table with 2 columnsTable---------------------KeyA KeyB1 A1 B2 B2 C3 C3 D4 E4 F5 G6 H7 I8 G9 A9 HOne can groups KeyA into 3 collections:1,2,3 & 945 & 6The groups are collection common elements of KeyB. Notice for example that 1 & 3 don’t have any common elements but still belong to the same group as they indirectly share elements in 2The Objective is to have a query that list all keyA’s that belong to the same collectionFor example: given KeyA = 2 (or 2 or 3) the selection should returnKeyA-------1239 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-04-14 : 16:24:29
|
My brain hurts reading this one.quote: Notice for example that 1 & 3 don’t have any common elements but still belong to the same group as they indirectly share elements in 2
HUH?1 A 1 B 2 B 2 C 3 C 3 D 1 has A,B2 has B,C3 has C,DWhat are you talking about? Brett8-) |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2003-04-14 : 16:33:35
|
| Huh?Aren't there four groups here:1,2,3,9,6 (A,B,C,D,H)4 (E,F)5,8 (G)7 (I)(I think he's talking about connected components of a bipartite graph, but I'm not sure!)Edited by - Arnold Fribble on 04/14/2003 16:34:32 |
 |
|
|
DanG
Starting Member
10 Posts |
Posted - 2003-04-14 : 16:33:55
|
quote: My brain hurts reading this one.quote: Notice for example that 1 & 3 don’t have any common elements but still belong to the same group as they indirectly share elements in 2
HUH?1 A 1 B 2 B 2 C 3 C 3 D 1 has A,B2 has B,C3 has C,DWhat are you talking about? Brett8-)
-----------------------------------------------------------------Well,1 has A,B2 has B,C3 has C,D9 has A,Hso 1 & 2 share B and 2 & 3 share C, hence 2 belongs to 1 & 3, hence 1, 2 & 3 are in the same collection9 does also share A, hence it also belongs to the same group.Logical, isn't it? ;-) |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-04-14 : 16:42:54
|
| You're connecting 1 and 3 because of their relationship to 2? This looks a lot like the adjancy model or a hierarchal one (parent/child). Although with your model it could almost be recursive.What problem is this meant to address, or is it just a mathmatical exercise? And how would you resolve recursion?Brett8-) |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-04-14 : 16:44:34
|
| If anyone can solve this, Arnold can!There's definitely a need for iteration or recursion here.declare @tA (keyA int)declare @tB (keyB char(1))declare @StartGroup int;declare @Done int;set @StartGroup =2set @Done = 0-- starting point:insert into @tAselect keya, keybfrom YourTable where KeyA = @StartGroup-- here, we loop:while done = 0 begininsert into @tB select keyB from YOurTable where KeyA in (select keya from @tA) and keyb not in (select keyb from @tB)insert into @tA select keyA from YourTablewhere keyB in (select KeyB from @tB) and keyA not in (select KeyA from @tA)if @@Rowcount = 0 set @done = 1endselect * from @tAsomething like that ... very ugly .... sorry don't have SQL Server running here to do any actual testing.- JeffEdited by - jsmith8858 on 04/14/2003 16:45:52 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-04-14 : 16:49:37
|
| I'm talking about having to gaurd against inifinte recursiveness (word?). You could get stuck in a loop ad infintum Unless I guess you eliminate values from your base table as you allocate them. But in the next breath, you may eliminate something that you might need later on for a different "collection". And it seems thing might be in mupltiple collections.I still want to know if this is a real world application or not.Brett8-) |
 |
|
|
DanG
Starting Member
10 Posts |
Posted - 2003-04-14 : 16:50:32
|
quote: You're connecting 1 and 3 because of their relationship to 2? This looks a lot like the adjancy model or a hierarchal one (parent/child). Although with your model it could almost be recursive.What problem is this meant to address, or is it just a mathmatical exercise? And how would you resolve recursion?Brett8-)
------------------This is a "simplification" of a real problem.I actually have 3 table with a many to many relationship. The table I've given is the in-between table.My tables represents invoices - OrderParts. You may have several OrderParts on an invoice and many invoice for an OrderPart.To have a total view I need the Orders and invoices that are in the same group as my example (tries to) show(s)... |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2003-04-14 : 16:51:59
|
quote: If anyone can solve this, Arnold can!
Damn, I was just thinking that Jeff would maybe chime in with a great solution...I hate graphs. Graphs are why I'm not Dr. Arnold Fribble, well, graphs and category theory and algebraic program transformation and laziness (both kinds). |
 |
|
|
DanG
Starting Member
10 Posts |
Posted - 2003-04-14 : 16:54:52
|
quote: If anyone can solve this, Arnold can!There's definitely a need for iteration or recursion here.declare @tA (keyA int)declare @tB (keyB char(1))declare @StartGroup int;declare @Done int;set @StartGroup =2set @Done = 0-- starting point:insert into @tAselect keya, keybfrom YourTable where KeyA = @StartGroup-- here, we loop:while done = 0 begininsert into @tB select keyB from YOurTable where KeyA in (select keya from @tA) and keyb not in (select keyb from @tB)insert into @tA select keyA from YourTablewhere keyB in (select KeyB from @tB) and keyA not in (select KeyA from @tA)if @@Rowcount = 0 set @done = 1endselect * from @tAsomething like that ... very ugly .... sorry don't have SQL Server running here to do any actual testing.- JeffEdited by - jsmith8858 on 04/14/2003 16:45:52
-----------------------------Yup, I've written something similar, but I was wondering if this could be achieved with a single select... |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-04-14 : 16:57:13
|
| Well, I wouldn't say simplified is the right word.Can you post your table ddl (preferable with PK and FK relationships).But it sounds like you have:InvoiceInvoiceIdcols..OrderPartsOrderIdPartIdcols..OrdersOrderIdcols..PartsPartsIdcols..And the Mother of all tables (your original post):InvoiceOrderPartsInvoiceIdOrderIdPartsIdcols...But I'm SURE however, you decided to use surrogate keys....Let us know. I know these guys and gals can figure it out (haven't seen them fail yet).Brett8-) |
 |
|
|
DanG
Starting Member
10 Posts |
Posted - 2003-04-14 : 16:57:53
|
quote: I'm talking about having to gaurd against inifinte recursiveness (word?). You could get stuck in a loop ad infintum Unless I guess you eliminate values from your base table as you allocate them. But in the next breath, you may eliminate something that you might need later on for a different "collection". And it seems thing might be in mupltiple collections.I still want to know if this is a real world application or not.-------------------No risk of that as the collections can not overlap. If there whould be any common elements, they would actually be part of a larger collectionBrett8-)
|
 |
|
|
DanG
Starting Member
10 Posts |
Posted - 2003-04-14 : 17:04:32
|
quote: Well, I wouldn't say simplified is the right word.Can you post your table ddl (preferable with PK and FK relationships).But it sounds like you have:InvoiceInvoiceIdcols..OrderPartsOrderIdPartIdcols..OrdersOrderIdcols..PartsPartsIdcols..And the Mother of all tables (your original post):InvoiceOrderPartsInvoiceIdOrderIdPartsIdcols...But I'm SURE however, you decided to use surrogate keys....Let us know. I know these guys and gals can figure it out (haven't seen them fail yet).Brett8-)
-----------------------------------Something like this:Invoice(InvoiceID) -< InvoiceLine(InvoiceID,InvoiceLineID)Order(OrderID) -< OrderPart(OrderId,OrderPartID)InvoiceLine -< cross(InvoiceID,InvoiceLineID,OrderId,OrderPartID) >- OrderPart |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-04-15 : 08:56:20
|
| DDL?I do see what you mean though...so it begs the question:What is your ultimate goal? Can you describe what you need from what tables? You seem to have a fairly straight forward ER Model....Brett8-) |
 |
|
|
DanG
Starting Member
10 Posts |
Posted - 2003-04-17 : 18:02:08
|
quote: DDL?I do see what you mean though...so it begs the question:What is your ultimate goal? Can you describe what you need from what tables? You seem to have a fairly straight forward ER Model....Brett8-)
---------------I need the collection of all Invoices of the same group, so I can totalize the lines on all related invoices and order parts.I solved this by adding columns to Invoice & OrderPart tables and some triggers to update them when modifications are made.This way queries are fast and the overhead limited. Of course a single select whould have been nicer, but life wouldn't be so nice if everything was too easy ;-) |
 |
|
|
|