This is the problem of using non-group columns in a grouped query. In fact, this can be avoided. Some DB mechanisms do not allow you to do this because it is not logical: if I want product.id for a group, but which one should use the DB mechanism because there are several options for one group?
ORDER BY is only for working with aggregates or grouped columns when using GROUP BY, but I'm not sure if you can easily aggregate. You are looking for product.id for a row with MIN (size_position) inside the group. I think that a subquery to isolate this row for each group may be the only way; I can't think of an easy way from my head to get it out.
EDIT
My instinct in these cases should always do this in two stages. I don’t really like gigantic clumsy queries with nested subqueries, simply because I find them unreadable. You have a very long query for a normalized schema, so I'm not going to try to break the query here, I will probably make a mistake.
Query 1 will call product_number, color_number and MIN (size_position) (grouping by product_number, color_number) in the temp table.
Query 2 will then retrieve the product_id for each product_number, color_number, size_position in the temp table. Thus, you can directly join the position of the minimum size.
The negative performance of using temporary tables is in many cases too high (not for everyone, but, of course, for 99% of the requests I have made over the past 10 years in trading applications).
There is another possibility related to the self-subordination of the subquery, but with such a strongly normalized scheme, I think that it would not be particularly beneficial to implement it.
source share