Cascading Database Queries

Status
Not open for further replies.

blargh

Ars Legatus Legionis
12,577
Subscriptor++
I've been doing some work in MS Access (it's what I have and what I know) basically rolling my own Bill Of Materials database. Assemblies, subassemblies, individual parts. Relatively straightforward stuff, a couple hundred rows.

It's easy enough to do a query for a particular assembly and get a list of the subassemblies and individual parts making it up. However, what I'd really like to do is iterate through each of those subassemblies and also list out them, down to the individual part level, to output an indented BOM of parts to order, or whatever. A treeview would be great, if that can be put into a report, but it would also be good to be able to output something to a flat Excel file for our purchasing people to deal with.

Can anyone provide some pointers on how to attack this problem? I can dive into the VBA side and get something working, I'm sure, but if there's an easier way to deal with it I'd love to hear it.
 

blargh

Ars Legatus Legionis
12,577
Subscriptor++
I have whatever comes built-in with Access 2010. There does seem to be a treeview control available, but at least with 2 minutes' effort I couldn't figure out how to connect it to a data source.

The part I'm trying to figure out is the cascading subquery. Some SQL pseudocode:

Code:
SELECT Assemblies.AssemPN, Assemblies.ChildPN, PNs.Description, Assemblies.Qty FROM Assemblies INNER JOIN PNs ON PNs.PartNumber = Assemblies.ChildPN WHERE AssemPN = foo

That returns a fine table result. But then I want to basically run the same query, with foo = ChildPN for each row. For individual parts it would return a null result, but for subassemblies it would give me the further breakdown.
 

Richard Berg

Ars Legatus Legionis
43,037
Subscriptor
Relations aren't super clear, but I *think* you want something like:

Code:
select ...
  from Assemblies
  inner join Parts mainParts 
    on Assemblies.AssemPN = mainParts.PN
  left join Parts subParts
    on Assemblies.ChildPN = subParts.PN

That takes care of fetching the data. But it's still a flat table, not a hierachical / indented / pivoted view. No idea how to wire it up to an Access tree view.
 

Richard Berg

Ars Legatus Legionis
43,037
Subscriptor
Richard Berg":2970ehir said:
Transitive closure isn't possible in (pure) SQL.
To clarify: it is possible if you can radically denormalize the schema, use loops/cursors, or have special "recursive" keywords. Some examples here: http://www.dba-oracle.com/t_sql_patterns_recursive.htm

But I doubt any of those options is available in your Access db.
 

tinyMan

Ars Tribunus Angusticlavius
6,149
Subscriptor++
In the past, I have done something similar to this by using a "ParentID" value in each row. Then the code that took the flat data from the DB and created the treeview would run through something like:

1. Loop through all records finding ParentID values that are null
2. Insert found records as top level of tree
3. Loop through all records finding ParentIDs in the set of "Top-Level ParentIDs"
4. Add all found nodes under their respective parents
5. Repeat until all records are consumed

This style, while mixing SQL and procedural code, allows for arbitrary grouping/tree style structures and only takes a single round-trip to the DB. The code loops are essentially "instant" when compared to an extra DB call.

The other option is recursive SQL requests where you fetch all children for an element, and then check each of those children for more children, etc. That does work on a small data set, but falls apart quickly at relatively small part counts (like <100).
 
Status
Not open for further replies.