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.

Extract the largest node in a path from multiple paths

Hi, I am new in neo4j.

I'm trying to query multiple list and find the max in each list.
The data are like
NA1 -> NA2 -> NA3 -> NA4 -> NB1 -> NB2 -> NB3 -> NB4 -> ......
(NAs had same attribute and NBs had same attribute)
I want to find the max id in NA1 to NA4, NB1 to NB4 and make sure the max id in each list is the first
e.g. NA1 is the max id in list NA
I tried

match p = (a:b:node {attribute:"NA"} )-[r*]->(b:node {attribute:"NA"})
WITH reduce(output = , n IN nodes(p) | output + n ) as nodeCollection

But I'm stuck here and don't know how to check the maximum ID in each list.
Or do anything in each list.

Thanks

1 ACCEPTED SOLUTION

You should be able to return a max number with Max() and then return the ID of that node so something like:

MATCH (n:Node)-[r*]->(b:Node {attribute: NA})
WHERE attribute = Max(attribute)
YOUR LOGIC HERE

That should at least get you to the max duration and then you can split off from there.

View solution in original post

8 REPLIES 8

MuddyBootsCode
Graph Steward

Hello Pawa, welcome to the community! Just to clarify, you're looking for the highest number attribute in your chain of node relationships? Or trying to find the node with the highest attribute and return it's ID?

Hi,
Thank for your apply!
I’m trying to find the highest number attribute in my chain of node and check whether it is the right position I want.
But the chain needs to split into small chain .
E.g..
NA1 -> NA2 -> NA3 -> NA4 -> NB1 -> NB2 -> NB3 -> NB4 ->
Split to
NA1 -> NA2 -> NA3 -> NA4
NB1 -> NB2 -> NB3 -> NB4

and check the max duration node is the number one node.
( N means node, A and the number are the attribute and each node has its own duration )
I want to check the max duration in every list is with attribute 1 like NA1 or NB1.
I can spirit the list like NA1 to NA4, NB1 to NB4.
But I don’t know how to find the max duration or check whether it is the position I want.

You should be able to return a max number with Max() and then return the ID of that node so something like:

MATCH (n:Node)-[r*]->(b:Node {attribute: NA})
WHERE attribute = Max(attribute)
YOUR LOGIC HERE

That should at least get you to the max duration and then you can split off from there.

Hi, thank for your apply again.

I know how to find the max duration in one path.
By using

with max(n.attribute) as max_d
return (n.attribute:max_d)

But when I match to multiple path and try to find the max duration on each path.
I don't know how to find.
This is the data I use.

MATCH p = (A:Task{FROMLOCTYPE:"LOAD_PORT"})-[:wafer_path*]->(b:Task{TOLOCTYPE:"LOAD_PORT"})
return p limit 3

I can group nodes according to the classification I want.
But I ’m not sure how to query for the max duration in each group with cypher.

MATCH p = (A:Task{FROMLOCTYPE:"LOAD_PORT"})-[:wafer_path*]->(b:Task{TOLOCTYPE:"LOAD_PORT"})
WITH reduce(output = , n IN nodes(p) | output + n ) as nodeCollection

Sorry my previous description was not clear enough.
Thank you again for your reply

When performing an aggregation, the non-aggregation variables present form the grouping key.

In your case, since you want the max attribute value per path, you need to have the path variable in scope and present in the WITH clause along with your aggregation:

...
with p, max(n.attribute) as max_d
...

Thank for your reply.

But when I use

MATCH p = (A:Task{FROMLOCTYPE:"LOAD_PORT"})-[:wafer_path*]->(b:Task{TOLOCTYPE:"LOAD_PORT"})
UNWIND NODES(p) AS node
RETURN MAX(node.duration) AS result

It will return the max duration of all nodes.
How can I take the maximum value in each P?

As previously mentioned, the grouping key you want must be present. You perform the aggregation in your RETURN, but you haven't provided any other variable to act as the grouping key, so it's performing a max() across all results. You need p to be present when you aggregate, so either include that in your RETURN along with your max(), or (if you don't want to return the path as well), perform the aggregation in a WITH clause (including p as the grouping key), and only then do your return:

MATCH p = (A:Task{FROMLOCTYPE:"LOAD_PORT"})-[:wafer_path*]->(b:Task{TOLOCTYPE:"LOAD_PORT"})
UNWIND NODES(p) AS node
WITH p, MAX(node.duration) AS result // p being present means it's the grouping key, so you get the max per path
RETURN result // if only the result is desired...otherwise add `p` here as well.

Thank you for your reply.
It really helped me.
Thanks again.