Discussion:
[sqlalchemy] aggregate_order_by() on multiple columns
pierre collet
2018-09-19 09:56:18 UTC
Permalink
Hello,

I am using SQLAlchemy on a PostgreSQL database and I was very pleased to
find the aggregate_order_by function (http://docs.sqlalchemy.org/ccept
ten/latest/dialects/postgresql.html#sqlalchemy.dialects.postgresql.aggregate_order_by
<http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#sqlalchemy.dialects.postgresql.aggregate_order_by>
).

Though, I am wondering why the "order_by" argument of the function takes
only one column while Postgresql definitely accepts to order by a set of
columns:

For instance:
SELECT array_agg(status ORDER BY pk_col1, pk_col2) FROM
table_with_pk_on_two_columns GROUP BY col_x;
works fine on my PostgreSQL 9.6 database.

Am I missing something or is this simply not yet implemented?

Thank you.
Pierre.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
Mike Bayer
2018-09-19 13:26:16 UTC
Permalink
Post by pierre collet
Hello,
I am using SQLAlchemy on a PostgreSQL database and I was very pleased to find the aggregate_order_by function (http://docs.sqlalchemy.org/ccept ten/latest/dialects/postgresql.html#sqlalchemy.dialects.postgresql.aggregate_order_by).
SELECT array_agg(status ORDER BY pk_col1, pk_col2) FROM table_with_pk_on_two_columns GROUP BY col_x;
works fine on my PostgreSQL 9.6 database.
Am I missing something or is this simply not yet implemented?
order_by can be turned into a "*order_by" if you can help by adding a
Post by pierre collet
from sqlalchemy.dialects.postgresql import aggregate_order_by
from sqlalchemy import column
from sqlalchemy.dialects import postgresql
from sqlalchemy.sql.expression import ClauseList
elem = aggregate_order_by(column('a'), ClauseList(column('b').asc(), column('c').desc()))
print(elem.compile(dialect=postgresql.dialect()))
a ORDER BY b ASC, c DESC
Post by pierre collet
Thank you.
Pierre.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
pierre collet
2018-09-19 16:43:12 UTC
Permalink
Thanks Mike for the trick :)

I have created an issue on Bitbucket as you asked here:
https://bitbucket.org/zzzeek/sqlalchemy/issues/4337/aggregate_order_by-doesnt-accept-multiple
.

Cheers,
Pierre
Post by pierre collet
Post by pierre collet
Hello,
I am using SQLAlchemy on a PostgreSQL database and I was very pleased to
find the aggregate_order_by function (http://docs.sqlalchemy.org/ccept
ten/latest/dialects/postgresql.html#sqlalchemy.dialects.postgresql.aggregate_order_by).
Post by pierre collet
Though, I am wondering why the "order_by" argument of the function takes
only one column while Postgresql definitely accepts to order by a set of
Post by pierre collet
SELECT array_agg(status ORDER BY pk_col1, pk_col2) FROM
table_with_pk_on_two_columns GROUP BY col_x;
Post by pierre collet
works fine on my PostgreSQL 9.6 database.
Am I missing something or is this simply not yet implemented?
order_by can be turned into a "*order_by" if you can help by adding a
Post by pierre collet
from sqlalchemy.dialects.postgresql import aggregate_order_by
from sqlalchemy import column
from sqlalchemy.dialects import postgresql
from sqlalchemy.sql.expression import ClauseList
elem = aggregate_order_by(column('a'), ClauseList(column('b').asc(),
column('c').desc()))
Post by pierre collet
print(elem.compile(dialect=postgresql.dialect()))
a ORDER BY b ASC, c DESC
Post by pierre collet
Thank you.
Pierre.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and
Verifiable Example. See http://stackoverflow.com/help/mcve for a full
description.
Post by pierre collet
---
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
Post by pierre collet
To unsubscribe from this group and stop receiving emails from it, send
<javascript:>.
Post by pierre collet
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
Loading...