# Extract the largest node in a path from multiple paths

**URL:** <https://community.neo4j.com/t/extract-the-largest-node-in-a-path-from-multiple-paths/14997>\
**Category:** Cypher\
**Created:** [February 23, 2020, 3:30pm UTC](https://community.neo4j.com/t/extract-the-largest-node-in-a-path-from-multiple-paths/14997 "2020-02-23T15:30:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pawa19961996](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/pawa19961996/32/8100_2.png) [@pawa19961996](https://community.neo4j.com/u/pawa19961996)\
**Post date:** [February 23, 2020, 3:30pm UTC](https://community.neo4j.com/t/extract-the-largest-node-in-a-path-from-multiple-paths/14997/1 "2020-02-23T15:30:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MuddyBootsCode](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/muddybootscode/32/10330_2.png) [@MuddyBootsCode](https://community.neo4j.com/u/MuddyBootsCode)\
**Post date:** [February 23, 2020, 5:26pm UTC](https://community.neo4j.com/t/extract-the-largest-node-in-a-path-from-multiple-paths/14997/2 "2020-02-23T17:26:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pawa19961996](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/pawa19961996/32/8100_2.png) [@pawa19961996](https://community.neo4j.com/u/pawa19961996)\
**Post date:** [February 23, 2020, 5:49pm UTC](https://community.neo4j.com/t/extract-the-largest-node-in-a-path-from-multiple-paths/14997/3 "2020-02-23T17:49:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MuddyBootsCode](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/muddybootscode/32/10330_2.png) [@MuddyBootsCode](https://community.neo4j.com/u/MuddyBootsCode)\
**Post date:** [February 24, 2020, 10:42am UTC](https://community.neo4j.com/t/extract-the-largest-node-in-a-path-from-multiple-paths/14997/4 "2020-02-24T10:42:11Z")

</div>

You should be able to return a max number with [Max()](https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#functions-max) and then return the ID of that node so something like:

```auto
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.

---

<div class="post-metadata">

**Author:** ![pawa19961996](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/pawa19961996/32/8100_2.png) [@pawa19961996](https://community.neo4j.com/u/pawa19961996)\
**Post date:** [February 24, 2020, 7:13pm UTC](https://community.neo4j.com/t/extract-the-largest-node-in-a-path-from-multiple-paths/14997/5 "2020-02-24T19:13:18Z")

</div>

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

 ![graph](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/a/a157cfc83954ff492664700eab5e22aa3876ac54.png)

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

---

<div class="post-metadata">

**Author:** ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)\
**Post date:** [February 25, 2020, 10:18pm UTC](https://community.neo4j.com/t/extract-the-largest-node-in-a-path-from-multiple-paths/14997/6 "2020-02-25T22:18:39Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![pawa19961996](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/pawa19961996/32/8100_2.png) [@pawa19961996](https://community.neo4j.com/u/pawa19961996)\
**Post date:** [February 26, 2020, 6:20am UTC](https://community.neo4j.com/t/extract-the-largest-node-in-a-path-from-multiple-paths/14997/7 "2020-02-26T06:20:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)\
**Post date:** [February 26, 2020, 7:46pm UTC](https://community.neo4j.com/t/extract-the-largest-node-in-a-path-from-multiple-paths/14997/8 "2020-02-26T19:46:36Z")

</div>

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:

```auto
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.

```

---

<div class="post-metadata">

**Author:** ![pawa19961996](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/pawa19961996/32/8100_2.png) [@pawa19961996](https://community.neo4j.com/u/pawa19961996)\
**Post date:** [February 28, 2020, 10:43am UTC](https://community.neo4j.com/t/extract-the-largest-node-in-a-path-from-multiple-paths/14997/9 "2020-02-28T10:43:38Z")

</div>

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