9 ms·
For the ORDER BY in grouped table (F868), it's kind of weird. I guess it's a special case with narrow usage. In the example, product_id is unique (as PK) alon
by ww520 4y ago
For the ORDER BY in grouped table (F868), it's kind of weird. I guess it's a special case with narrow usage.
In the example, product_id is unique (as PK) along with product_code being unique. There's one product_id for each product_code. A Group By on product_id will produce a list of groups where each group has one product_id and one product_code. A subsequent Order By on the groups by the product_code would work. I assume if there're multiple product_code's per product_id, the query won't work.
CREATE TABLE product (
product_id int PRIMARY KEY,
product_name varchar,
product_code varchar UNIQUE
);
SELECT product.product_id, sum(product_part.num) ...
GROUP BY product.product_id
ORDER BY product.product_code;