'sqlalchemy dynamic filter conditions using python dictionary

source table: t1 has many rows for the same id. We need to filter by dt and get min(dt)

data = [{'id': 1, 'dt': '2009-01-01'}, {'id': 2, 'dt': '2009-02-01'}, {'id': 3, 'dt': '2009-01-03'}]

how do I represent this to the following query? Basically dynamically filter based on the condition. Essentially, its like joining another table (t2) based on t1.id = t2.id and t1.dt > t2.dt

select id, min(dt) from t1
where (id = 1 and dt > '2009-01-01') or (id = 2 and dt > '2009-01-02') or (id = 3 and dt > '2009-01-03') 
group by id;


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source