Let's say I have the following table:
create table orders (id int, order_id int, vendor varchar, price int, quantity int); INSERT INTO orders values (1, 1, 'nike', 10, 2), (2, 2, 'nike', 10, 3), (3, 3, 'adidas', 15, 1), (4, 3, 'adidas', 15, 5);
I want to get the sum of the price per distinct order_id and total sum of the quantity grouped by vendor. So the answer here would be:
| vendor | sum_price | sum_quantity | | nike | 20 | 5 | | adidas | 15 | 6 |
Notice how the sum of the price for 'adidas' is only 15, because they both have the same order_id, so it'll only take one. In all cases, the one to choose whenever multiple order_id's are the same doesn't matter because it will always be associated with the same price. Is it also possible to do this without a nested query?