Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Hi, I have the follwing table.PRODUCTS TABLEProductID------InternalProductCode-------Price-----Name1--------------101-----------------------100-------TestProduct2--------------101-----------------------200-------TestProduct3--------------101-----------------------300-------TestProduct4--------------102-----------------------100-------TestProduct2I would like to select ProductID, InternalProductCode, Name and the Max price where the InternalProductCode is the same. So my result would look like this.ProductID------InternalProductCode-------MaxPrice-----Name1--------------101-----------------------300-------TestProduct2--------------101-----------------------300-------TestProduct3--------------101-----------------------300-------TestProduct4--------------102-----------------------100-------TestProduct2What is wrong with this statement? ThanksSelectProductID,InternalProductCode,Name,(Select Max(Price) From Products Where InternalProductCode = InternalProductCode) As MaxPrice,From Products
Page47
Master Smack Fu Yak Hacker
2878 Posts
Posted - 2002-08-08 : 13:55:44
You need to alias your table names so the subquery knows what you are talking about...
Select ProductID, InternalProductCode, Name, (Select Max(Price) From Products p2 Where p.InternalProductCode = p2.InternalProductCode) As MaxPrice, From Products p
Jay White{0}Edited by - Page47 on 08/08/2002 13:56:48