Search code examples
pythonsqlalchemygreatest-n-per-group

SqlAlchemy group_by and return max date


I have a table such has

identifier date        value
A          2017-01-01  2 
A          2017-01-02  1
A          2017-01-03  7
B          2017-01-01  2 
B          2017-01-02  7
B          2017-01-03  3

I am trying to select the max date of each identifier such as I get :

identifier date        value
A          2017-01-03  7
B          2017-01-03  3

thank you


Solution

  • Using a subquery:

    SELECT t1.identifier, t1.date, t1.value FROM table t1
    JOIN
    (
        SELECT identifier, MAX(date) maxdate
        FROM table
        GROUP BY identifier
    ) t2
    ON t1.identifier = t2.identifier AND t1.date = t2.maxdate;
    

    In SQLAlchemy:

    from sqlalchemy import func, and_
    
    subq = session.query(
        Table.identifier,
        func.max(Table.date).label('maxdate')
    ).group_by(Table.identifier).subquery('t2')
    
    query = session.query(Table).join(
        subq,
        and_(
            Table.identifier == subq.c.identifier,
            Table.date == subq.c.maxdate
        )
    )