Skip to content

GROUP_CONCAT(x ORDER BY y) sorts by x, not by y #2658

Open

Description

@mathiasrw

GROUP_CONCAT(x ORDER BY y) sorts by x, not by y. Only the direction of the first ORDER BY item is used; the expression is ignored.

Reproduced on develop:

const people = [{name: 'ann', age: 30}, {name: 'bob', age: 20}, {name: 'cid', age: 25}];

alasql('SELECT VALUE GROUP_CONCAT(name ORDER BY age) FROM ?', [people]);
// 'ann,bob,cid'   expected 'bob,cid,ann' (MySQL; string_agg in PostgreSQL)

Cause

getGroupConcatParams in src/423groupby.js (added in #2418) passes only col.order[0].direction to the aggregator, which then sorts the collected values themselves.

Direction

Collect each value together with its sort key (all ORDER BY items, evaluated per row), sort by the keys at the end, then join. Ordering by the value itself (GROUP_CONCAT(name ORDER BY name)) stays a special case of this.

This fixes a wrong result nobody can rely on, so it can go into 4.x.

Tests: the example above, DESC, two ORDER BY items, ORDER BY the value itself, and together with SEPARATOR.

Related: #2413, #2418.

Activity

  1. mateuscmtropical commented on Nov 11, 2026

    @mateuscmtropical
    Contributor

    Hi, I'd like to work on this one.

  2. mateuscmtropical commented on Nov 11, 2026

    @mateuscmtropical
    Contributor

    Hi, I've opened a PR for this: #2995

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions