# Aggregate functions for paths

**URL:** <https://community.neo4j.com/t/aggregate-functions-for-paths/28824>\
**Category:** Cypher\
**Tags:** cypher\
**Created:** [November 11, 2020, 1:09pm UTC](https://community.neo4j.com/t/aggregate-functions-for-paths/28824 "2020-11-11T13:09:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![julian.n.de.jonge](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/julian.n.de.jonge/32/3870_2.png) [@julian.n.de.jonge](https://community.neo4j.com/u/julian.n.de.jonge)\
**Post date:** [November 11, 2020, 1:09pm UTC](https://community.neo4j.com/t/aggregate-functions-for-paths/28824/1 "2020-11-11T13:09:33Z")

</div>

Hi all,

I've got a query that finds some paths through a Marking relationship, where the "Marking" relationship contains a property "dt\_val" (datetime).

What I would like to do, is for each path, get the largest "dt\_val" value in that path. I thought that would be done with the following query:

```auto
MATCH p = (s)-[r:Marking*]->(t)
RETURN p, max([rel in relationships(p)|rel.dt_val]) as highest_dt

```

However, this returns a list of all values of "dt\_val" as opposed to the largest one. When I remove p, this returns a list of 4 items, but the number of paths is much higher than 4. I believe this is because the function is being applied in the wrong direction in this matrix; 4 in this case is the largest number of :Marking relationships that exists per path.

Is anyone able to spot why it isn't returning the maximum, based on the list comprehension, for each path?

Many thanks,  
Nick

---

<div class="post-metadata">

**Author:** ![terryfranklin82](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/terryfranklin82/32/5233_2.png) [@terryfranklin82](https://community.neo4j.com/u/terryfranklin82)\
**Post date:** [November 11, 2020, 9:16pm UTC](https://community.neo4j.com/t/aggregate-functions-for-paths/28824/2 "2020-11-11T21:16:26Z")

</div>

> [@julian.n.de.jonge](#):
>
> ```auto
> max([rel in relationships(p)|r.dt_val]) as highest_dt
> 
> ```

Should this not read:

```auto
max([rel in relationships(p)|rel.dt_val]) as highest_dt

```

---

<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:** [November 12, 2020, 1:39am UTC](https://community.neo4j.com/t/aggregate-functions-for-paths/28824/3 "2020-11-12T01:39:06Z")

</div>

The problem is `max()` is an aggregation function, not a scaler function, it works across rows, not across values in a list.

Cypher doesn't currently have a scaler max() function, but APOC Procedures does in the form of `apoc.coll.max()`. Install APOC and try this:

```auto
MATCH p = (s)-[r:Marking*]->(t)
RETURN p, apoc.coll.max([rel in relationships(p) | rel.dt_val]) as highest_dt

```

Be aware that `p` will match to every path possible (including smaller segments of larger paths) that uses :Marking relationships. If you wanted to only evaluate paths where `s` is a start node (with no incoming :Marking rels) and `t` is an end node (with no outgoing :Marking rels), then you should add that restriction as a WHERE clause:

```auto
MATCH p = (s)-[r:Marking*]->(t)
WHERE NOT ()-[:Marking]->(s) AND NOT (t)-[:MARKING]->()
RETURN p, apoc.coll.max([rel in relationships(p) | rel.dt_val]) as highest_dt

```

---

<div class="post-metadata">

**Author:** ![julian.n.de.jonge](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/julian.n.de.jonge/32/3870_2.png) [@julian.n.de.jonge](https://community.neo4j.com/u/julian.n.de.jonge)\
**Post date:** [November 12, 2020, 2:36pm UTC](https://community.neo4j.com/t/aggregate-functions-for-paths/28824/4 "2020-11-12T14:36:26Z")

</div>

Correct, thanks. Have updated

---

<div class="post-metadata">

**Author:** ![julian.n.de.jonge](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/julian.n.de.jonge/32/3870_2.png) [@julian.n.de.jonge](https://community.neo4j.com/u/julian.n.de.jonge)\
**Post date:** [November 12, 2020, 2:37pm UTC](https://community.neo4j.com/t/aggregate-functions-for-paths/28824/5 "2020-11-12T14:37:33Z")

</div>

Thanks Andrew, that's done the trick. My neo installation is on an offline machine, and the match query is quite large (and irrelevant in this case) so didn't include the bulk of it. Much appreciated!
