[Answer]-Complex count based on the latest date of a month

1👍

✅

This can likely be done with .extra(), add this to the query:

.extra(
    where=["""history_date = (SELECT MAX(history_date) FROM historical_record hr
                              WHERE hr.history_id = historical_record.history_id
                                    AND hr.history_date < %s)"""],
    params=[month_end]
)

Leave a comment