简体   繁体   中英

Translating SQL sub-query to Peewee ORM

I have a problem similar to Translate SQLite query, with subquery, into Peewee statement and Can peewee nest SELECT queries such that the outer query selects on an aggregate of the inner query? .

The result I'm trying to generate is: given a fruit table with rows of (type, variety, price) , find the cheapest variety of each fruit. There are a couple of solutions described at http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/ which work:

select f.type, f.variety, f.price
from (
   select type, min(price) as minprice
   from fruits group by type
) as x inner join fruits as f on f.type = x.type and f.price = x.minprice;

Or:

select type, variety, price
from fruits
where price = (select min(price) from fruits as f where f.type = fruits.type);

How can I Peewee-ify one or both of these?

Cribbing from http://charlesleifer.com/blog/techniques-for-querying-lists-of-objects-and-determining-the-top-related-item/ , I have:

subquery = (Fruits
            .select(
                Fruits.type.alias('type'),
                fn.Min(Fruits.price).alias('min'))
            .group_by(Fruits.type)
            .alias('subquery'))

query = (Fruits
         .select()
         .join(subquery, on=(
             (Fruits.price == subquery.c.min) &
             (Fruits.type == subquery.c.type)
        )))

This works, but I don't understand everything it's doing. What's going on with subquery.c and why is the subquery aliased?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM