You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
MIN() and MAX() functions not returning values for dates #2453
I am testing out alasql for SharePoint lists reports. It looks like MIN() and MAX() are not working for dates (but things like COUNT() and YEAR() work so I know the date format is OK). I'm not 100% sure but I think the issue maybe that alasql is using JavaScript Math functions for aggregates, which do not support dates. I would expect this to work as it is very basic functionality for reports and dashboards.
I made a temporary fix in my project by adding two new functions, like this:
// ==========================================
// ALASQL CUSTOM AGGREGATORS
// ==========================================
alasql.aggr.MAXDATE = function(v, s, stage) {
if (stage === 1) return v;
if (stage === 2) {
if (v == null || v === '') return s;
if (s == null || s === '') return v;
return (v > s) ? v : s;
}
if (stage === 3) return s;
};
alasql.aggr.MINDATE = function(v, s, stage) {
if (stage === 1) return v;
if (stage === 2) {
if (v == null || v === '') return s;
if (s == null || s === '') return v;
return (v < s) ? v : s;
}
if (stage === 3) return s;
};
I would encourage you to implement some kind of support for dates in the aggregate functions MIN, MAX, FIRST, LAST, whether it's a fix similar to the above or an automatic conversion of date to number+unix epoch or similar.
I don't know if the maintainers are around anymore... or maybe they're just busy. I would say, if you know how to fix it, then go for it and send them a pull request.
Hi, I hadn't noticed that version 4.17 has a slightly different implementation. According to Claude Opus, this is the fix needed:
GREATEST / LEAST (scalar) Change needed: Filter nulls; use .getTime() for Date comparison.
stdlib.GREATEST = stdlib.MAX = function () {
var args = Array.prototype.slice.call(arguments);
return '(function(){ ' +
'var vals = [' + args.join(',') + '].filter(function(x){ return x !== null && typeof x !== "undefined"; }); ' +
'if(!vals.length) return undefined; ' +
'return vals.reduce(function(a,b){ ' +
'var av = a instanceof Date ? a.getTime() : a; ' +
'var bv = b instanceof Date ? b.getTime() : b; ' +
'return av > bv ? a : b; }); ' +
'})()';
};
stdlib.LEAST = stdlib.MIN = function () {
var args = Array.prototype.slice.call(arguments);
return '(function(){ ' +
'var vals = [' + args.join(',') + '].filter(function(x){ return x !== null && typeof x !== "undefined"; }); ' +
'if(!vals.length) return undefined; ' +
'return vals.reduce(function(a,b){ ' +
'var av = a instanceof Date ? a.getTime() : a; ' +
'var bv = b instanceof Date ? b.getTime() : b; ' +
'return av < bv ? a : b; }); ' +
'})()';
};
Reasoning: ANSI SQL specifies that GREATEST/LEAST return NULL when all arguments are NULL, and skip NULLs otherwise. JavaScript's > / < against null coerces it to 0, producing wrong results. Dates need .getTime() for correct numeric comparison.
ANSI SQL: Current non-compliant → after fix compliant.R: pmax/pmin with na.rm=TRUE happen to match, but that is incidental.
Activity
itsLohit commentedon Apr 1, 2026
Hi! I'd like to work on a fix for this.
My plan:
Math.min/Math.maxin the MIN/MAX aggregators with</>comparison operators, which work correctly with Date values
Let me know if there's anything specific you'd like me to do differently!
gabrielmongefranco commentedon Apr 6, 2026
I don't know if the maintainers are around anymore... or maybe they're just busy. I would say, if you know how to fix it, then go for it and send them a pull request.
gabrielmongefranco commentedon Apr 19, 2026
Hi, I hadn't noticed that version 4.17 has a slightly different implementation. According to Claude Opus, this is the fix needed:
GREATEST / LEAST (scalar)
Change needed: Filter nulls; use .getTime() for Date comparison.
Reasoning: ANSI SQL specifies that GREATEST/LEAST return NULL when all arguments are NULL, and skip NULLs otherwise. JavaScript's > / < against null coerces it to 0, producing wrong results. Dates need .getTime() for correct numeric comparison.
ANSI SQL: Current non-compliant → after fix compliant.R: pmax/pmin with na.rm=TRUE happen to match, but that is incidental.
gabrielmongefranco commentedon Apr 19, 2026
Closing here and moving discussion to #2481 , which includes similar issues with other aggregate functions in the same file.
gabrielmongefranco commentedon Apr 19, 2026
Closing here and moving discussion to #2481 , which includes similar issues with other aggregate functions in the same file.