Sort by a column in a union query in SqlAlchemy SQ

2020-04-14 07:21发布

As explained in this question, you can use string literals to do order by in unions.

For example, this works with Oracle:

querypart1 = select([t1.c.col1.label("a")]).order_by(t1.c.col1).limit(limit)
querypart2 = select([t2.c.col2.label("a")]).order_by(t2.c.col2).limit(limit)
query = querypart1.union_all(querypart2).order_by("a").limit(limit)

The order-by can take a string literal, which is the name of the column in the union result.

(There are gazillions of rows in partitioned tables and I'm trying to paginate the damn things)

When running against SQLite3, however, this generates an exception:

sqlalchemy.exc.OperationalError: (OperationalError) near "ORDER": syntax error

How can you order by the results of a union?

1条回答
▲ chillily
2楼-- · 2020-04-14 07:54

The queries that are part of a union query must not be sorted.

To be able to use limits inside a compound query, you must wrap the individual queries inside a separate subquery:

SELECT * FROM (SELECT ... LIMIT ...)
UNION ALL
SELECT * FROM (SELECT ... LIMIT ...)
q1 = select(...).limit(...).subquery()
q2 = select(...).limit(...).subquery()
query = q1.union_all(q2)...
查看更多
登录 后发表回答