GROUP_CONCAT, and similar functions

1,099 views
Skip to first unread message

James Hooker

unread,
Mar 21, 2014, 4:45:29 AM3/21/14
to peewe...@googlegroups.com
Hey - loving the library, very cool, but I have a quick question.

I've searched through the docs, and the source, but can see no mention of using functions like 'GROUP_CONCAT'. Should I just execute a raw query against a result set in order to use actions like this?

I know these functions are not neccesarily available across all DB Engines, so can understand why you wouldn't include support by default.

Thanks

p.s. I am using sqlite3 currently as my DB Engine

Charles Leifer

unread,
Mar 21, 2014, 11:28:09 AM3/21/14
to peewe...@googlegroups.com
You can use `fn`:

from peewee import fn

fn.Group_Concat(...)

fn.Count(...)
fn.Substr(fn.Lower(..)...)


--
You received this message because you are subscribed to the Google Groups "peewee-orm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to peewee-orm+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Hooker

unread,
Mar 21, 2014, 1:44:49 PM3/21/14
to peewe...@googlegroups.com

Hi Charles,

That's great, thanks! I was already using fn for 'count', but I guess I didn't fully grasp this functionality.

Thanks for the clarification!

James H

You received this message because you are subscribed to a topic in the Google Groups "peewee-orm" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/peewee-orm/AM76ojd3XNs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to peewee-orm+...@googlegroups.com.

A.L. Kleijngeld

unread,
May 4, 2014, 10:03:46 AM5/4/14
to peewe...@googlegroups.com
Hi,

This post was certainly useful to me, but I was wondering if it possible to pass along an alternative separator character? Using MySQL, I now have:

fn.Group_Concat(Person.first_name, ' ', Person.last_name).alias('directors')

Which results in "Satoshi Kon,Shôgo Furuya". I would like to use the seperator ', ' (including the space, so it would result in "Satoshi Kon, Shôgo Furuya". In MySQL this is done with the seperator keyword.

Thank you!

Charles Leifer

unread,
May 5, 2014, 9:51:41 AM5/5/14
to peewe...@googlegroups.com
Yeah this gets a little bit gross because the function call joins its arguments with commas, but the "SEPARATOR <foo>" bit needs to go after the last argument.  Here is one way that might work:

fn.GROUP_CONCAT(Person.first_name, Clause(Person.last_name, SQL("SEPARATOR ', '")))

And another way that might:

fn.GROUP_CONCAT(
  Clause(
    CommaClause(Person.first_name, Person.last_name), 
    SQL("SEPARATOR ', '")  # CommaClause is multiple nodes joined by commas
  )  # Clause is multiple nodes joined by spaces
)


--
Reply all
Reply to author
Forward
0 new messages