hi,
I'm trying to write a query. I want to display that which product most sold in related year. And i want to show like this,
YEAR, PRODUCTNAME, MAXQUANTITY
1996 Gorgonzola Telino 444
1997 Gnocchi di nonna Alice 971
1998 Konbu 659
But my code (following code) is listing all products' quantity, but i want to see only maximum products in related year.
SELECT ODATE,ProductName,MAXQ FROM Products P INNER JOIN
(
SELECT T.ODATE,T.ProductID,MAX(T.QUANT) AS MAXQ
FROM
(
SELECT YEAR(O.OrderDate) ODATE,ProductID,SUM(QUANTITY) AS QUANT FROM [Order Details] OD
INNER JOIN Orders O ON O.OrderID=OD.OrderID
GROUP BY YEAR(O.OrderDate),ProductID
) T
group by T.ODATE,T.ProductID
) T2 ON P.ProductID=T2.ProductID
ORDER BY ODATE ASC,MAXQ DESC
But if i add TOP 1 with ties
to first select, its getting only one year info.
YEAR, PRODUCTNAME, MAXQUANTITY
1996 Gorgonzola Telino 444
How can i solve that problem? Can u advice me something please?