flâneur — a map of the web's best reading

Sqlalchemy complex queries and subqueries · adam-gligor

adam-gligor.github.io · 696 words · saved by 1 readers

My brain already understands sql syntax so I choose something that reads like sql, it’s just my personal preference not the only syntax. The following exercise showcases: The model: One vendor can have one or more domains and revenue tracks the daily revenues per domain Problem Find the vendors and the domains with highest revenue that contributed to 90% of the revenue of the past 7 days Generated sql: This problem can be solved in more than one way and this not necessary the best way, but it’s complex enough to serve as an example for the syntax. Sqlalchemy docs

Sqlalchemy complex queries and subqueries 15 Nov 2019 Here’s how I put together a complex query in sqlalchemy using subqueries. Approach My brain already understands sql syntax so I choose something that reads like sql, it’s just my personal preference not the only syntax. query or subquery = session.query( [select fields] ).select_from( [left_side] ).join( [right side or subquery] ).join( .... ).[other clauses] [.subquery()] Example The following exercise showcases: aggregation filtering, sorting table aliasing column aliasing sub queries The model: class Vendor ( Base ): __tablename__ = 'ven

Explore this link on the map →

related reading