SQL which LEFT JOIN with top 1

I'm looking for SQL query which do the LEFT JOIN with top 1

Select p.*, w2.Wid
from product p
 LEFT JOIN
                  (
                        Select  top 1 Location, Qty, Wid
                        From Warehouse w
                        where w.ProductID = p.ProductID
                        Order by Qty desc
                  ) w2 ON w.ProductID = p.ProductID

Product
-------------------
ProductID             Name
ABC                        zzz
EFG                        yyy
HID                        xxx


Warehouse
---------
Wid  ProductID      Qty
1    ABC        25
2    ABC        35
3    EFG        25
4    EFG        NULL
5    EFG        26
6    HID            NULL
7    HID            NULL



Result should be
-----------------
ProductID             Name      Wid
ABC                        zzz            2
EFG                        yyy            5
HID                        xxx              6
Bharat GuruAsked:
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.

Dustin SaundersDirector of OperationsCommented:
You can get a ROW_NUMBER() off the table as ordered by Qty and then join based off that criteria.

SELECT p.ProductId,p.Name,w2.Wid FROM
Product p
INNER JOIN
	(SELECT ROW_NUMBER() OVER (PARTITION BY ProductID ORDER BY Qty Desc) AS "RN",ProductID,Wid,Qty
	FROM Warehouse
	) w2 ON w2.ProductId = p.ProductID AND w2.RN = 1

Open in new window


Full example:
CREATE TABLE #Product (ProductID nvarchar(50),Name nvarchar(50))
CREATE TABLE #Warehouse (Wid int, ProductId nvarchar(50), Qty int)

INSERT INTO #Product (ProductID,Name)
VALUES ('ABC','zzz'),('EFG','yyy'),('HID','xxx')

INSERT INTO #Warehouse (Wid, ProductId, Qty)
VALUES (1,'ABC',25),(2,'ABC',35),(3,'EFG',25),(4,'EFG',NULL),(5,'EFG',26),(6,'HID',NULL),(7,'HID',NULL)

SELECT p.ProductId,p.Name,w2.Wid FROM
#Product p
INNER JOIN
	(SELECT ROW_NUMBER() OVER (PARTITION BY ProductID ORDER BY Qty Desc) AS "RN",ProductID,Wid,Qty
	FROM #Warehouse
	) w2 ON w2.ProductId = p.ProductID AND w2.RN = 1

DROP TABLE #Product
DROP TABLE #Warehouse

Open in new window


Results:
ABC	zzz	2
EFG	yyy	5
HID	xxx	6

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
Bharat GuruAuthor Commented:
Thanks
0
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
SQL

From novice to tech pro — start learning today.