cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

RETURN variable number of column

Hello, everyone,

I have the following problem: I have modeled different bills of material, each with a different number of sub-items. Now my question is, I want to output the BOM with all items on the lowest level, I have already solved that, but I want to know all names of the corresponding upper assembly. So...
(Assembly)-[has]-(Assembly)-[has]-(Product)
(assembly)-[has]-(product)
Now each assembly sometimes has 2 and sometimes 3 or more sub-assemblies and in the last place always the product.
I would like to have the return:
subassembly | subassembly (if any)... | subassembly (if any)... | Product

Maybe someone can help me. Thank you very much.
Greetings
Julian

4 REPLIES 4

Are you looking to output a String consisting of subassemblies separated by | characters with the Product as the last part of the string?

Or are you looking to output a fixed number of variables/columns with each corresponding to a subassembly (if it exists) with the Product as the last one? Or something else?

Hi,
yes i am looking for a String consisting of subassemblies separated by | characters with the Product as the last part of the string.

This might get you started.

A variable-length relationship will provide the traversals needed through all subassemblies, ending in the product. If you have a path variable, you can extract the nodes from the path. You can use apoc.text.join() from APOC Procedures to join up the values with | symbols between them.

So assuming the name property is desired for output, and is present on both assemblies and the product, maybe something like this:

MATCH path = (start:Assembly)-[:has*]->(:Product)
WHERE NOT ()-[:has]->(start) // ensure start is the start of the chain, not in the middle
 AND all(node in nodes(path)[..-1] WHERE node:Assembly) // for all nodes except the last
RETURN apoc.text.join([node in nodes(path) | node.name], ' | ') as output

Hello @julian-hartmann

We will need more details to write a query but I can give you some tips to make your query:

Regards,
Cobra

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online