如何在sqlalchemy中划分两列

时间:2019-06-18 13:56:28

标签: python sqlalchemy

如何使用sqlalchemy生成此SQL

SELECT SUM(click) / SUM(impression) as my_indicator
FROM stats;
session.query(
    func.sum(table.click).label("click"),
    func.sum(table.impression).label("impression"),
    # ??? sum(click) / sum(impression) ???
).group_by(table.type)

1 个答案:

答案 0 :(得分:0)

from sqlalchemy.sql import label

session.query(
    label('my_indicator', sum(click) / sum(impression))
).group_by(table.type)