Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
02-23-2020 07:30 AM
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
Solved! Go to Solution.
02-24-2020 02:42 AM
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.
02-23-2020 09:26 AM
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?
02-23-2020 09:49 AM
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.
02-24-2020 02:42 AM
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.
02-24-2020 11:13 AM
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
02-25-2020 02:18 PM
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
...
02-25-2020 10:20 PM
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?
02-26-2020 11:46 AM
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.
02-28-2020 02:43 AM
Thank you for your reply.
It really helped me.
Thanks again.
All the sessions of the conference are now available online