cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Is there an Average Duration? If not can we put a request in for such a concept?

I am trying to get the average duration between two time stamps and it appears Cypher cannot do this. Can we get a change request for future versions to include this?

2 REPLIES 2

So I found this here: https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#functions-avg-duration saying you can use avg on duration but when I run it on 3.49 I get the following error:

Neo.ClientError.Statement.SyntaxError: Type mismatch: expected Float or Integer but was Duration (line 1, column 103 (offset: 102)) "Match (u:User) with u limit 100 with duration.between(u.first_obs,u.last_obs) as d return avg(d)" ^

My guess is this was not implemented until 3.5. I easy work around is to do some simple math to get the query to think of the data as a float:

Match (u:User) with u limit 100 with duration.between(u.first_obs,u.last_obs) as d return avg(d.days+tofloat(d.minutes)/1440)

your link to https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#functions-avg-duration refers to 3.5.x documentation. In the URL current will always refer to the most recent version of Neo4j. However you can replace current in the URL with 3.4 or 3.3 etc to look at prior versions of the documentation

I was able to run your query as-is under 3.5.8 and did not encounter error

 ./cypher-shell
Connected to Neo4j 3.5.8 at bolt://localhost:7687.
Type :help for a list of available commands or :exit to exit the shell.
Note that Cypher queries must end with a semicolon.
neo4j> Match (u:User) with u limit 100 with duration.between(u.first_obs,u.last_obs) as d return avg(d);
+--------+
| avg(d) |
+--------+
| NULL   |
+--------+

admittedly I have no data.
But alsov https://github.com/neo4j/neo4j/wiki/Neo4j-3.5-changelog#350-alpha02 indicates

Cypher
 * Aggregation (SUM, AVG) for Durations
Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online