Just so I understand what you're asking?

You want the minimum price per product (across all colors), as well as the
list of colors?
A result set looking something like this?

| Attribute | Min Price | Colors           |
|-----------|-----------|------------------|
| Attr_1    | 10        | [blue,red,green] |
| Attr_2    | 200       | [violet,scarlet] |
| Attr_3    | 30        | [orange, yellow] |

Or do you want the min price per color, attribute combination?


On Wed, Sep 4, 2019 at 10:00 PM Andrew M <and...@mackiefamily.org> wrote:

> Hi,
>
> I would like to use distinct inside a group_by query, e.g.:
>
> session.query(Product.attribute_x, func.min(Product.price),
> distinct(Product.color)). group_by(Product.attribute_x)
>
> That is, for each value of attribute_x, I want to find the lowest price
> and all of the colors available.
>
> I don't seem to be able to use distinct in this way. Can anyone please
> suggest an efficient method of identifying the distinct colors for each
> value of attribute_x (without creating a new query for each value of
> attribute_x)?
>
> Thank you,
> Andrew
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sqlalchemy/949655c4-eacb-46fd-ae68-48a7aa1a3446%40googlegroups.com
> <https://groups.google.com/d/msgid/sqlalchemy/949655c4-eacb-46fd-ae68-48a7aa1a3446%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/CADm-oGmUcMMAkWs6mpCNTS%2BxfieUrKoNBbg8myesXP8AJbq%2Bnw%40mail.gmail.com.

Reply via email to