Skip to content
Snippets Groups Projects
Commit 18209bda authored by colemanw's avatar colemanw
Browse files

SearchKit - Fix default aggregate function for grand total rows

parent af820d3e
No related branches found
No related tags found
No related merge requests found
......@@ -340,22 +340,20 @@
fieldToColumn: fieldToColumn,
// Supply default aggregate function appropriate to the data_type
getDefaultAggregateFn: function(info) {
var ret = {flag_before: ''};
var arg = info.args[0] || {};
if (arg.suffix) {
return null;
}
switch (info.data_type) {
case 'Integer':
// For the `id` field, default to COUNT, otherwise SUM
ret.fnName = (info.args[0] && info.args[0].field && info.args[0].field.name === 'id') ? 'COUNT' : 'SUM';
break;
return (!info.fn && arg.field && arg.field.name === 'id') ? 'COUNT' : 'SUM';
case 'Float':
ret.fnName = 'SUM';
break;
default:
ret.fnName = 'GROUP_CONCAT';
ret.flag_before = 'DISTINCT ';
case 'Money':
return 'SUM';
}
return ret;
return null;
},
// Find all possible search columns that could serve as contact_id for a smart group
getSmartGroupColumns: function(api_entity, api_params) {
......
......@@ -330,8 +330,9 @@
if (ctrl.canAggregate(col)) {
// Ensure all non-grouped columns are aggregated if using GROUP BY
if (!info.fn || info.fn.category !== 'aggregate') {
var dfl = searchMeta.getDefaultAggregateFn(info);
ctrl.savedSearch.api_params.select[pos] = dfl.fnName + '(' + dfl.flag_before + fieldExpr + ') AS ' + dfl.fnName + '_' + fieldExpr.replace(/[.:]/g, '_');
var dflFn = searchMeta.getDefaultAggregateFn(info) || 'GROUP_CONCAT',
flagBefore = dflFn === 'GROUP_CONCAT' ? 'DISTINCT ' : '';
ctrl.savedSearch.api_params.select[pos] = dflFn + '(' + flagBefore + fieldExpr + ') AS ' + dflFn + '_' + fieldExpr.replace(/[.:]/g, '_');
}
} else {
// Remove aggregate functions when no grouping
......
......@@ -67,7 +67,7 @@
_.each(ctrl.display.settings.columns, function(col) {
if (col.type === 'field') {
col.tally = {
fn: searchMeta.getDefaultAggregateFn(searchMeta.parseExpr(col.key)).fnName
fn: searchMeta.getDefaultAggregateFn(searchMeta.parseExpr(ctrl.parent.getExprFromSelect(col.key)))
};
}
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment