# Find max duration in each group

**URL:** <https://community.neo4j.com/t/find-max-duration-in-each-group/15087>\
**Category:** Cypher\
**Created:** [February 26, 2020, 8:46am UTC](https://community.neo4j.com/t/find-max-duration-in-each-group/15087 "2020-02-26T08:46:25Z")\
**Posts on this page:** 6\
**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 26, 2020, 8:46am UTC](https://community.neo4j.com/t/find-max-duration-in-each-group/15087/1 "2020-02-26T08:46:25Z")

</div>

I want to find the max duration in different attribute.  
This is my data format.

> (:task{WAFERID, duration, FORMLOC, TOLOC})

| WAFERID | duration | FROMLOC | TOLOC |
| --- | --- | --- | --- |
| A1 | 10 | L1 | L2 |
| A1 | 15 | L2 | L3 |
| A1 | 12 | L3 | L4 |
| B1 | 9 | L1 | L2 |
| B1 | 12 | L2 | L3 |
| B1 | 15 | L3 | L4 |
| C1 | 9 | L1 | L2 |
| C1 | 12 | L2 | L3 |
| C1 | 15 | L3 | L4 |

Each node records the work of wafer move.  
I want to find the longest duration job for each wafer id?  
e.g..

| WAFERID | duration | FROMLOC | TOLOC |
| --- | --- | --- | --- |
| A1 | 15 | L2 | L3 |
| B1 | 15 | L3 | L4 |
| C1 | 15 | L3 | L4 |

---

<div class="post-metadata">

**Author:** ![12kunal34](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/12kunal34/32/2617_2.png) [@12kunal34](https://community.neo4j.com/u/12kunal34)\
**Post date:** [February 26, 2020, 9:36am UTC](https://community.neo4j.com/t/find-max-duration-in-each-group/15087/2 "2020-02-26T09:36:32Z")

</div>

Hi @pawa19961996 ,

Please find below query in your case which will find the max duration.

```auto
MATCH(g:task)
WITH g.WAFERID AS ID, collect(m.duration) AS duration
UNWIND duration AS val
RETURN ID, max(val) as max_duration

```

Please let me know if any other help required.🙂

---

<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, 11:30am UTC](https://community.neo4j.com/t/find-max-duration-in-each-group/15087/3 "2020-02-26T11:30:29Z")

</div>

Oh my god Kunal, thanks for your reply it works like a amazing!

But I still don't fully understand how it works.

> WITH g.WAFERID AS ID, collect(m.duration) AS duration

Why the duration match the ID?

Thanks again.  
Have a nice day!

---

<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, 12:19pm UTC](https://community.neo4j.com/t/find-max-duration-in-each-group/15087/4 "2020-02-26T12:19:06Z")

</div>

Or if I want to find the max of each relation.

> [rel:chamber\_path{time}]

The chamber path will record the move of wafer.

> (a:Task)-[:chamber\_path]-\>(Task{FROMLOC: a.FROMLOC})

| id | WAFERID | duration | FROMLOC | TOLOC |
| --- | --- | --- | --- | --- |
| 1 | A1 | 10 | L1 | L2 |
| 2 | A1 | 15 | L2 | L3 |
| 3 | A1 | 12 | L3 | L4 |
| 4 | B1 | 9 | L1 | L2 |
| 5 | B1 | 12 | L2 | L3 |
| 6 | B1 | 15 | L3 | L4 |
| 7 | C1 | 9 | L1 | L2 |
| 8 | C1 | 12 | L2 | L3 |
| 9 | C1 | 15 | L3 | L4 |

Chamber path

| id | duration | FROM\_ID | TO\_ID |
| --- | --- | --- | --- |
| 1 | 10 | 1 | 4 |
| 2 | 15 | 4 | 7 |
| 3 | 12 | 2 | 5 |
| 4 | 9 | 5 | 8 |
| 5 | 12 | 3 | 6 |
| 6 | 15 | 6 | 9 |

How can I find the max duration in each chamber path?

> (a:Task)-[:chamber\_path\*]-\>(:Task{FROMLOC:a.FROMLOC)

e.g..

| id | duration | FROM\_ID | TO\_ID |
| --- | --- | --- | --- |
| 2 | 15 | 4 | 7 |
| 3 | 12 | 2 | 5 |
| 6 | 15 | 6 | 9 |

---

<div class="post-metadata">

**Author:** ![12kunal34](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/12kunal34/32/2617_2.png) [@12kunal34](https://community.neo4j.com/u/12kunal34)\
**Post date:** [February 26, 2020, 12:28pm UTC](https://community.neo4j.com/t/find-max-duration-in-each-group/15087/5 "2020-02-26T12:28:57Z")

</div>

I believe your other question is not clear.  
could you please tell us your requirement in better way if possible 🙂

and here is an update on the above answer.

```auto
MATCH(g: task)
RETURN g.ID, max(g.duration) as max_duration

```

---

<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:48am UTC](https://community.neo4j.com/t/find-max-duration-in-each-group/15087/6 "2020-02-28T10:48:05Z")

</div>

Thank for your reply.  
I found the cypher to query my question.

> MATCH(g:Task)-[r:chamber\_path]-\>(b:Task{FROMLOCTYPE:g.FROMLOCTYPE})  
> WITH g.PPID AS PPID, collect(r.time) AS duration,g.FROMLOCTYPE as FROMLOCTYPE  
> UNWIND duration AS val  
> WITH max(val) as max\_d,min(val) as min\_d,FROMLOCTYPE, max(val) - min(val) as time\_different  
> return min\_d,max\_d, time\_different,PPID,FROMLOCTYPE order by time\_different DESC

I use the method similar to searching for nodes.  
Thank you very much for your reply.  
It really helps me.
