# Find the largest node of each segment in a series of paths

**URL:** <https://community.neo4j.com/t/find-the-largest-node-of-each-segment-in-a-series-of-paths/15006>\
**Category:** Cypher\
**Created:** [February 24, 2020, 6:46am UTC](https://community.neo4j.com/t/find-the-largest-node-of-each-segment-in-a-series-of-paths/15006 "2020-02-24T06:46:31Z")\
**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 24, 2020, 6:46am UTC](https://community.neo4j.com/t/find-the-largest-node-of-each-segment-in-a-series-of-paths/15006/1 "2020-02-24T06:46:31Z")

</div>

I'm trying to divide different short chains according to the head and tail I want in a long chain, and find the max duration in the short chains.

E.g..  
Long chain:  
NA1 -\> NA2 -\> NA3 -\> NA4 -\> NB1 -\> NB2 -\> NB3 -\> NB4 -\>...

I want to check whether the max duration in each chain is the second node.  
NA1 -\> NA2 -\> NA3 -\> NA4  
NB1 -\> NB2 -\> NB3 -\> NB4  
( N means node, A,b and the number are the attribute and each node has its own duration )

MATCH p = (A:Task{FROMLOCTYPE:"1"})-[:path\*]-\>(b:Task{TOLOCTYPE:"4"})  
WITH reduce(output = , n IN nodes(p) | output + n ) as tasks

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

---

<div class="post-metadata">

**Author:** ![intouch\_vivek](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/intouch_vivek/32/4097_2.png) [@intouch\_vivek](https://community.neo4j.com/u/intouch_vivek)\
**Post date:** [February 24, 2020, 2:55pm UTC](https://community.neo4j.com/t/find-the-largest-node-of-each-segment-in-a-series-of-paths/15006/2 "2020-02-24T14:55:09Z")

</div>

Hi,  
Could you please provide small dataset and the logic to break the big chain to smaller ones.

Regards  
Vivek

---

<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, 5:22pm UTC](https://community.neo4j.com/t/find-the-largest-node-of-each-segment-in-a-series-of-paths/15006/3 "2020-02-24T17:22:34Z")

</div>

> [@pawa19961996](#):
>
> Long Chain dataset

This is the sample for my data.

 ![problem](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/9/94404c8ea19b85e1332090b9ff66af29ef51eeae.png)  
Each node has FROMLOCTYPE,TOLOCTYPE,DURATION and Program.

I want to confirm that all nodes under the same program control, its longest time is in the second position (from B to C).

Thanks again for your reply.

---

<div class="post-metadata">

**Author:** ![intouch\_vivek](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/intouch_vivek/32/4097_2.png) [@intouch\_vivek](https://community.neo4j.com/u/intouch_vivek)\
**Post date:** [February 24, 2020, 6:10pm UTC](https://community.neo4j.com/t/find-the-largest-node-of-each-segment-in-a-series-of-paths/15006/4 "2020-02-24T18:10:17Z")

</div>

I inserted your sample data as below

| A | B | 10 | typeA |
| --- | --- | --- | --- |
| B | C | 12 | typeA |
| C | D | 9 | typeA |
| D | A | 2 | typeA |
| A | B | 11 | typeB |
| B | C | 19 | typeB |
| C | D | 10 | typeB |
| D | A | 5 | typeB |
| A | B | 2 | typeC |
| ![image | 690x282](upload://2QZQci5kzESaOhSTVpIZtkxivfr.png) | | |

\*\*and applied Query as \*\*  
unwind ["typeA","typeB","typeC"] as typeOf match p= (from:Chain)-[rel:CALLED{type:typeOf}]-\>(to:Chain) with rel.type as type, max(tointeger(rel.duration)) as maxDur return type, maxDur

\*\*Output \*\*

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/9/930c7a91a1c57117970adece2b45be37385d2c35.png)

---

<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:14pm UTC](https://community.neo4j.com/t/find-the-largest-node-of-each-segment-in-a-series-of-paths/15006/5 "2020-02-24T19:14:54Z")

</div>

Hi, thank for your apply again.

Sorry my previous description was not clear enough.

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:** ![intouch\_vivek](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/intouch_vivek/32/4097_2.png) [@intouch\_vivek](https://community.neo4j.com/u/intouch_vivek)\
**Post date:** [February 25, 2020, 12:37pm UTC](https://community.neo4j.com/t/find-the-largest-node-of-each-segment-in-a-series-of-paths/15006/6 "2020-02-25T12:37:02Z")

</div>

Hi,

So you can see that my code is performing based on the type of relationship..  
Do you want to find the query dynamically?

---

<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 25, 2020, 3:34pm UTC](https://community.neo4j.com/t/find-the-largest-node-of-each-segment-in-a-series-of-paths/15006/7 "2020-02-25T15:34:29Z")

</div>

Hi, thank for your apply again.  
Yes I want to find the query dynamically.

I think my problem should be when I find a bunch of lists.  
How do I search these nodes at the same time.  
A bit like how to find the maximum value of an array in a two-dimensional array.

I want to find the max number in each list I find.

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

---

<div class="post-metadata">

**Author:** ![intouch\_vivek](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/intouch_vivek/32/4097_2.png) [@intouch\_vivek](https://community.neo4j.com/u/intouch_vivek)\
**Post date:** [February 25, 2020, 4:41pm UTC](https://community.neo4j.com/t/find-the-largest-node-of-each-segment-in-a-series-of-paths/15006/8 "2020-02-25T16:41:55Z")

</div>

> [@intouch\_vivek](#):
>
> unwind ["typeA","typeB","typeC"] as typeOf match p= (from:Chain)-[rel:CALLED{type:typeOf}]-\>(to:Chain) with rel.type as type, max(tointeger(rel.duration)) as maxDur return type, maxDur

I am not very sure if below query is what you looking for  
Dynamic type can be coded as below  
match (from:Chain)-[rel:CALLED]-\>(to:Chain) with distinct rel.type as xyz unwind xyz as typeOf match p= (from:Chain)-[rel:CALLED{type:typeOf}]-\>(to:Chain) with rel.type as type, max(tointeger(rel.duration)) as maxDur return type, maxDur

If your requirement is different, then please frame your output and I will see if I am able to help you

---

<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:51am UTC](https://community.neo4j.com/t/find-the-largest-node-of-each-segment-in-a-series-of-paths/15006/9 "2020-02-26T06:51:10Z")

</div>

Thanks for your reply.

This is helpful for me.  
But if I don't want to use unwind ["typeA","typeB","typeC"] as typeOf.  
How can I match the all type of the relation and unwind it?
