duration::min(array<duration>) and duration::max(array<duration>) database functions
Author: EllisVerhoestCreated Jan 22, 2026Updated Sep 16, 2026
Labelswontfixtopic:database_functions/surrealdbGitHub Feature
What component does this affect?
SurrealDB Server
Is this feature related to a problem?
N/A
Suggest a solution
duration::min([1w, 2w]); // 1w
duration::max([1w, 2w]); // 2wAlternatively math::min and math::max could be modified to allow for any numerical esque datatype, including duration and datetime off the top of my head.
Additionally, duration::mean and duration::sum would also be useful, and could also be included in the math functions math::mean and math::sum
Alternative methods
For version 3.0+
DEFINE FUNCTION OVERWRITE fn::duration::max($durations: array<duration>) {
LET $durations_nanos = $durations.map(|$_| duration::nanos($_));
RETURN duration::from_nanos(
math::max($durations_nanos)
)
};
RETURN fn::duration::max([1w, 4w, 3w]);
-------- Query 1 (9.249948ms) --------
NONE
-------- Query 2 (2.715024ms) --------
4wThe above function works in 2.4/5 also, but GROUP BY ALL queries will pass the individual values to it instead of the whole array of values, so to get the same result you'd have to subquery.
SELECT fn::duration::max(duration) as duration FROM ONLY [
{duration: 69s69ms},
{duration: 1s},
{duration: 1y1ms69ns},
{duration: 342s},
{duration: 50w69ms100ns},
] GROUP ALL LIMIT 1;
-------- Query 1 (387.497µs) --------
'Expected a array<duration> but found 1m9s69ms'SurrealDB version
3.0.0-nightly+20260120.61a8d3e for linux on x86_64
Contact Details
email: [email protected] | discord: ellis_verhoest_aspirecomps
Is there an existing issue for this?
- I have searched the existing issues
Code of Conduct
- I agree to follow this project's Code of Conduct
Source: surrealdb/surrealdb