Friday, 26 October 2018

How to perform date_trunc query in Postgres using SQLAlchemy

I can't seem to be able to translate the following query into SQLAlchemy.

I would like to translate the following query:

SELECT date_trunc('day', time), "PositionReport".callsign FROM tvh_aircraft."PositionReport" WHERE "PositionReport".reg = 'PH-BVA'
GROUP BY 1, "PositionReport".callsign

I've tried the following, but with no luck.

flight_days = session\
        .query(PositionReport)\
        .filter(PositionReport.reg == reg) \
        .group_by(func.date_trunc('day', PositionReport.time))\
        .group_by('1')\
        .all()

    trunc_date = func.date_trunc('day', PositionReport.time)
    flight_days = session.query(trunc_date, PositionReport.callsign) \
        .filter(PositionReport.reg == reg) \
        .group_by("date_trunc_1")

Thanks in advance for your help.



from How to perform date_trunc query in Postgres using SQLAlchemy

No comments:

Post a Comment