the client, the product you buy code more times (max sod.OrderQty)

Hi experts

i have this query

SELECT  [CustomerID], sod.ProductID, sod.OrderQty
  FROM [Sales].[SalesOrderHeader] soh
            join [Sales].[SalesOrderDetail] sod
            on soh.[SalesOrderID] = sod.[SalesOrderID]
where [CustomerID] = 29921
  order by [CustomerID], sod.ProductID, sod.OrderQty desc

this is the results
CustomerID      ProductID            OrderQty
29921                  707                        4
29921                  707                        3
29921                  707                        2
29921                  708                        3
29921                  708                        1
29921                  711                        3
29921                  711                        3
29921                  711                        1
29921                  712                        3
29921                  712                        3
29921                  712                        2
29921                  712                        1

i need this results
CustomerID      ProductID            OrderQty
29921                  707                        4
29921                  708                        3
29921                  711                        3
29921                  712                        3

a customer can buy several products, the products may be the same, I need the client, the product you buy code more times (max sod.OrderQty)
enrique_aeoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
try:

select * from
(
SELECT  [CustomerID], sod.ProductID, sod.OrderQty,
row_number() over (partition by sod.ProductID order by sod.OrderQty desc) idx
  FROM [Sales].[SalesOrderHeader] soh
            join [Sales].[SalesOrderDetail] sod
            on soh.[SalesOrderID] = sod.[SalesOrderID]
where [CustomerID] = 29921
) a
where a.idx = 1
  order by a.[CustomerID], a.ProductID, a.OrderQty desc

ROW_NUMBER (Transact-SQL)
https://msdn.microsoft.com/en-sg/library/ms186734.aspx
0
Mike EghtebasDatabase and Application DeveloperCommented:
Select cID, pID, oQ
From (SELECT  row_number() Over(Partition By [CustomerID], sod.ProductID Order By OrderQty Desc) As rn 
               , [CustomerID] As cID, sod.ProductID As pID, sod.OrderQty As oQ
  FROM [Sales].[SalesOrderHeader] soh
            join [Sales].[SalesOrderDetail] sod
            on soh.[SalesOrderID] = sod.[SalesOrderID]
where [CustomerID] = 29921) As D (cID, pID, oQ, rn)
Where rn=1
  order by cID, pID

Open in new window


If you change  [CustomerID] = 29921 to include more customers like:

[CustomerID] in ( 29921, 29922, 29923) for example, you will need

...(Partition By [CustomerID], sod.ProductID ...
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
SELECT  [CustomerID], sod.ProductID, MAX(sod.OrderQty)
FROM [Sales].[SalesOrderHeader] soh
             join [Sales].[SalesOrderDetail] sod
             on soh.[SalesOrderID] = sod.[SalesOrderID]
WHERE [CustomerID] = 29921
GROUP BY [CustomerID], sod.ProductID
ORDER BY [CustomerID], sod.ProductID

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.