I guess this query may help you as per your requirement.
Declare @OrderDetails Table
(
OrderID Int Identity(1,1),
ProductId Int,
OrderQty Int
)
Insert @OrderDetails(ProductId,OrderQty)
Select 1,2 Union All
Select 1,1 Union All
Select 1,4 Union All
Select 2,2 Union All
Select 2,2 Union All
Select 2,5 Union All
Select 2,1 Union All
Select 3,2 Union All
Select 3,1 Union All
Select 3,1 Union All
Select 3,5 Union All
Select 4,2 Union All
Select 5,2 Union All
Select 5,2 Union All
Select 6,2 Union All
Select 6,2 Union All
Select 6,2 ;
Select ProductId From (
Select Dense_Rank() Over (Order By Sum(OrderQty)) As RwNum,ProductId,Sum(OrderQty) AS TotalCnt
From @OrderDetails
Group By ProductId
) Data
Where RwNum <=10
Please have look on the comment
No comments:
Post a Comment