Thomas Drake wrote:
>
> Wow, my above example sucked. Here's something close to what I'm
> actually using:
>
> qs = ...
> qunion = qs[0].union_all(*qs[1:]).subquery()
>
> joined = session.query( KnownComponents.name,
>                         qunion,
>                         func.group_concat(qunion.c.status.op('order
> by')(qunion.c.stop))
>                         ). \
>             join(qunion).filter
> (KnownComponents.id==qunion.c.known_components_id)
> joined = joined.group_by( KnownComponents.name,
>                           KnownComponents.version,
>                           qunion.c.serial,
>                           qunion.c.dependent_version,
>                           qunion.c.compile_type )
> rows = joined.all()

op() is old school.  make a custom expression element:


from sqlalchemy.ext import compiler
from sqlalchemy.sql import ColumnElement

class group_concat(ColumnElement):
    def __init__(self, col1, col2):
        self.col1 = col1
        self.col2 = col2
        self.type = col1.type

@compiler.compiles(group_concat, 'mysql')
def compile_group_concat(element, compiler, **kw):
    return "GROUP CONCAT(%s ORDER BY %s)" % (
                compiler.process(element.col1),
                compiler.process(element.col2)
           )


from sqlalchemy import *

m = MetaData()
t = Table('t1', m, Column('foo', String), Column('bar', String))

print select([group_concat(t.c.foo,
t.c.bar)]).compile(bind=create_engine('mysql://'))

"SELECT GROUP CONCAT(t1.foo ORDER BY t1.bar) AS anon_1"


> >
>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to