{"id":"c6ed14e14c88c55b","repo":"go-redis/redis","slug":"redis-groupby-is-not-allowed-when-multiple-aggreg","errorCode":null,"errorMessage":"redis: GROUPBY is not allowed when multiple aggregators are specified","messagePattern":"redis: GROUPBY is not allowed when multiple aggregators are specified","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"timeseries_commands.go","lineNumber":170,"sourceCode":"\tcase StdS:\n\t\treturn \"STD.S\"\n\tcase VarP:\n\t\treturn \"VAR.P\"\n\tcase VarS:\n\t\treturn \"VAR.S\"\n\tcase Twa:\n\t\treturn \"TWA\"\n\tcase CountNaN:\n\t\treturn \"COUNTNAN\"\n\tcase CountAll:\n\t\treturn \"COUNTALL\"\n\tdefault:\n\t\treturn \"\"\n\t}\n}\n\nvar (\n\terrTSMultiAggregationGroupBy = errors.New(\"redis: GROUPBY is not allowed when multiple aggregators are specified\")\n\terrTSAggregationConflict     = errors.New(\"redis: setting both Aggregator and Aggregators is not allowed; use Aggregators instead because Aggregator is deprecated\")\n\terrTSExcludeEmptyGroupBy     = errors.New(\"redis: EXCLUDEEMPTY is not allowed with GROUPBY\")\n)\n\nfunc formatAggregationArgs(aggregator Aggregator, aggregators []Aggregator) (string, int, error) {\n\tif aggregator != Invalid && len(aggregators) > 0 {\n\t\treturn \"\", 0, errTSAggregationConflict\n\t}\n\tif len(aggregators) == 0 {\n\t\tif aggregator == Invalid {\n\t\t\treturn \"\", 0, nil\n\t\t}\n\t\taggregationArg, err := formatAggregatorArg(aggregator)\n\t\tif err != nil {\n\t\t\treturn \"\", 0, err\n\t\t}\n\t\treturn aggregationArg, 1, nil\n\t}","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/go-redis/redis/blob/36d97525cd8076aed67cddf54778e9ea84550929/timeseries_commands.go#L152-L188","documentation":"Returned (set on the cmd) by TSMRangeWithArgs/TSMRevRangeWithArgs when both multiple aggregators are specified (Aggregators slice with >1 entry) AND GroupByLabel or Reducer is set. Redis TS.MRANGE does not support GROUPBY across multiple aggregations in one query; go-redis rejects this combination client-side. multiAggregationCount > 1 combined with a grouping option triggers it.","triggerScenarios":"Setting TSMRangeOptions.Aggregators to a slice of 2+ aggregators while also setting GroupByLabel or Reducer. Mixing the new multi-aggregator API with the GROUPBY/REDUCE clustering options.","commonSituations":"Building dashboards that aggregate multiple metrics (multi-aggregator) and also want label grouping. Migrating from single Aggregator to Aggregators without removing the GroupByLabel/Reducer fields. Config-driven query builders that enable both features simultaneously.","solutions":["Choose one: either use multiple aggregators WITHOUT GroupByLabel/Reducer, or use a single aggregator WITH grouping.","If you need grouped multi-aggregation, issue separate TSMRange queries per aggregator and group each.","Set GroupByLabel and Reducer to nil when Aggregators has more than one entry."],"exampleFix":"// before — multiple aggregators + grouping (rejected)\nopts := &redis.TSMRangeOptions{\n    Aggregators:  []redis.Aggregator{redis.Avg, redis.Max},\n    GroupByLabel: \"host\",\n}\nclient.TSMRangeWithArgs(ctx, from, to, filters, opts)\n\n// after — single aggregator with grouping\nopts := &redis.TSMRangeOptions{\n    Aggregator:   redis.Avg,\n    GroupByLabel: \"host\",\n}\n// or drop grouping and keep multiple aggregators\nopts := &redis.TSMRangeOptions{\n    Aggregators: []redis.Aggregator{redis.Avg, redis.Max},\n}","handlingStrategy":"validation","validationCode":"func validateTSMRangeOpts(o *redis.TSMRangeOptions) error {\n    multi := 0\n    if o.Aggregator != redis.Invalid {\n        multi = 1\n    }\n    if len(o.Aggregators) > 0 {\n        multi = len(o.Aggregators)\n    }\n    if multi > 1 && (o.GroupByLabel != nil || o.Reducer != nil) {\n        return errors.New(\"GROUPBY/REDUCE not allowed with multiple aggregators\")\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"cmd := c.TSMRangeWithArgs(ctx, from, to, filters, opts)\nif err := cmd.Err(); err != nil {\n    if err.Error() == \"redis: GROUPBY is not allowed when multiple aggregators are specified\" {\n        // drop grouping or reduce to a single aggregator\n    }\n}","preventionTips":["Do not combine Aggregators (>1) with GroupByLabel/Reducer.","For grouped multi-metric dashboards, issue separate queries per aggregator.","Centralize TSMRange option validation in a query builder."],"tags":["timeseries","validation","aggregation","api-misuse","client-side"],"analyzedSha":"36d97525cd8076aed67cddf54778e9ea84550929","analyzedAt":"2026-08-06T01:08:27.376Z","schemaVersion":2}