使用sqlalchemy.func.rownumber().over
生成 PARTITION BY 时,如何将其更改为 ORDER BY 。
query = session.query(
sqlalchemy.func.row_number().over(Record.id).label('#'),
Record.id,
Record.state,
).filter(
Record.state == "new"
)
sqlalchemy.engine.base.Engine使用 PARTITION BY
生成SELECT row_number() OVER (PARTITION BY records.pk) AS "#", records.pk AS records_pk, records.state AS records_state
FROM records
WHERE records.state = %(state_1)s
答案 0 :(得分:2)
我相信over()
函数应该使用order_by
参数:
query = session.query(
sqlalchemy.func.row_number().over(order_by=Record.id).label('#')
Record.id,
Record.state,
).filter(
Record.state == "new"
)
答案 1 :(得分:0)
您可以简单地执行以下操作:
select records.pk AS records_pk, records.state AS records_state
FROM records
WHERE records.state = %(state_1)s
Group by records.pk, records.state
order by records.pk, records.state
使用分组依据。 GROUP BY子句是聚合的,而PARTITION BY子句是解析的。