Im fiddling around with w3schools northwind DB. https://www.w3schools.com/sql/trymysql.asp?filename=trysql_func_mysql_count
What im trying to accomplish: A result like this:
orderid suppliername numberOfProducts
10300 New Orleans Cajun Delights 2
The problem here: The respective order actually requires two DIFFERENT suppliers. This isnt reflected in the result to my query.
Here is the query:
SELECT o.orderid, s.suppliername, COUNT(p.productname) AS numberOfProducts
FROM Suppliers s
JOIN Orders o
ON o.shipperid = s.supplierid
JOIN OrderDetails od
ON od.orderid = o.orderid
JOIN Products p
ON p.productid = od.productid
GROUP BY o.orderid, s.suppliername
HAVING o.orderid = 10300;
I'm rather confused. As far as I could find on SO, grouping by multiple columns is possible in mysql: GROUP BY behavior when no aggregate functions are present in the SELECT clause
The "priorization" goes from left to right in the GROUP BY clause. So out of the above query, I would expect the following behavior:
First, group as many identical resultsets as possible for the column O.OrderID
into one row.
Second, group as many identical results as possible for the column S.SupplierName
into one row.
For each resulting row, aggregate the number of resultsets for the column P.ProductName
.
But currently, it does only half the job. It basically forgets to group the individual resultsets for the column S.SupplierName
accordingly.
So here I only see one Supplier supplying us with 2 products for this order, while in reality, it is two suppliers, each of them supplying us with one single product for the order.
What am I missing?