Any way to SELECT GROUP_CONCAT(DISTINCT column)?

Is there a way to use GROUP_CONCAT() function in the SELECT statement? Was looking for something like

from sql import Table

t = Table("t")
t.select(functions.GroupConcat(t.column, distinct=True))

but can’t seem to find a single function that alludes to performing a GROUP_CONCAT().

GROUP_CONCAT is not a SQL standard function (it seems to exist only on MySQL).
But you can create your own version of aggregate expression like:

from sql.aggregate import Aggregate

class GroupConcat(Aggregate):
    __slots__ = ()
   _sql = 'GROUP_CONCAT'

But of course this will work only on MySQL database.

Thank you for the quick response.
As for

it seems to exist only on MySQL

it does also exist in SQLite.

But you can create your own version of aggregate expression like:

I’m sorry if this is obvious but is there a documentation for this? I would also like to add DISTINCT to it if possible.

Hum. SQLite is not well-known to follow standard neither :wink:

The code is the documentation.
The distinct option is automatically inherited from Aggregate class like many others.

The code is the documentation.
The distinct option is automatically inherited from Aggregate class like many others.

I see. I’ll be sure to read the source code then, thank you.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.