on 2020 Dec 16 3:55 PM
Hi all, I've done a query that gives me the highest sales value for each category for each country. But I only want to know what is the category that sold the highest for each countr. This is my query that gives me those values, but without the last filter for the highest category for each country, and since I can't do a MAX (SUM(od.unitprice * od.Quntity) I was wondering if there is a Subquery I could use
Select country, categoryname, SUM(od.unitprice * od.Quantity) as Valor From Categories c Join Products p on c.CategoryID = p.CategoryID Join [Order Details] od on p.ProductID = od.ProductID Join Orders o on od.orderID = o.OrderID Join Customers c2 on o.CustomerID = c2.CustomerID Group by Country, CategoryName, year(o.OrderDate) Having year(o.OrderDate) = 1997
Request clarification before answering.
Hi, thank you all for the answers, I finally did it doing a partition, the result is here:
Select * from (Select ROW_NUMBER() OVER(PARTITION BY COUNTRY ORDER BY SUM(od.unitprice * od.Quantity) DESC) AS 'ROW_NUMBER', country, categoryname, SUM(od.unitprice * od.Quantity) as Valor From Categories c Join Products p on c.CategoryID = p.CategoryID Join [Order Details] od on p.ProductID = od.ProductID Join Orders o on od.orderID = o.OrderID Join Customers c2 on o.CustomerID = c2.CustomerID Group by Country, CategoryName, year(o.OrderDate) Having year(o.OrderDate) = 1997) as tbl Where tbl.ROW_NUMBER = 1
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
41 | |
15 | |
10 | |
9 | |
6 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.