How to execute SELECT DISTINCT ON query using SQLAlchemy

时间:2015-10-06 08:16:21

标签: postgresql flask sqlalchemy

I have a requirement to display spend estimation for last 30 days. SpendEstimation is calculated multiple times a day. This can be achieved using simple SQL query:

SELECT DISTINCT ON (date) date(time) AS date, resource_id , time
FROM spend_estimation
WHERE
  resource_id = '<id>'
  and time > now() - interval '30 days'
ORDER BY date DESC, time DESC;

Unfortunately I can't seem to be able to do the same using SQLAlchemy. It always creates select distinct on all columns. Generated query does not contain distinct on.

query = session.query(
    func.date(SpendEstimation.time).label('date'),
    SpendEstimation.resource_id,
    SpendEstimation.time
).distinct(
    'date'
).order_by(
    'date',
    SpendEstimation.time
)
SELECT DISTINCT
    date(time) AS date,
    resource_id,
    time 
FROM spend
ORDER BY date, time

It is missing ON (date) bit. If I user query.group_by - then SQLAlchemy adds distinct on. Though I can't think of solution for given problem using group by.


Tried using function in distinct part and order by part as well.

query = session.query(
    func.date(SpendEstimation.time).label('date'),
    SpendEstimation.resource_id,
    SpendEstimation.time
).distinct(
    func.date(SpendEstimation.time).label('date')
).order_by(
    func.date(SpendEstimation.time).label('date'),
    SpendEstimation.time
)

Which resulted in this SQL:

SELECT DISTINCT
       date(time) AS date,
       resource_id,
       time,
       date(time) AS date # only difference
FROM spend
ORDER BY date, time

Which is still missing DISTINCT ON.

2 个答案:

答案 0 :(得分:1)

您的SqlAlchemy版本可能是罪魁祸首。

Sqlalchemy with postgres. Try to get 'DISTINCT ON' instead of 'DISTINCT'

此错误报告的链接: https://bitbucket.org/zzzeek/sqlalchemy/issues/2142

修复程序没有向后移植到0.6,看起来修复为0.7。

答案 1 :(得分:0)

愚蠢的问题:您是否尝试过SpendEstimation.date而不是'date'

编辑:让我感到震惊的是你正试图使用​​SELECT中的命名列。 SQLAlchemy并不那么聪明。尝试将func表达式传递到distinct()来电。