In my query I need to find the supplier with the highest costs for every single year.
SELECT YEAR(ORDERS.OrderDate),
MAX(ORDERS.Freight) AS [Greatest cost]
FROM ORDERS
GROUP BY YEAR(ORDERS.OrderDate)
ORDER BY YEAR(ORDERS.OrderDate) ASC
This code does give me the maximum cost per year, it doesn't give me the name of the supplier.
SELECT YEAR(ORDERS.OrderDate),
SHIPPERS.ShipperID,
SHIPPERS.CompanyName,
MAX(ORDERS.Freight) AS [Greatest cost]
FROM ORDERS, SHIPPERS
WHERE SHIPPERS.ShipperID = ORDERS.ShipVia
GROUP BY YEAR(ORDERS.OrderDate),
SHIPPERS.ShipperID,
SHIPPERS.CompanyName
ORDER BY YEAR(ORDERS.OrderDate) ASC
This code then gave me too much, as in, it gave me all the suppliers (with their highest numbers) for every single year, while I need the highest supplier per year.
Thanks in advance!
JOINsyntax in the ANSI-92 SQL Standard (more than 20 years ago) and its use is discouraged20and a second supplier had three orders of10each, which supplier do you want for that year?