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.
My query is"SELECT [ResultID],[HotelSearchResults].[HotelID],[HotelName],p.minPriceFROM [Hotels].[dbo].[HotelSearchResults]INNER JOIN(SELECT HotelID, MIN(Price) AS minPriceFROM HotelSearchResultsGROUP BY HotelID)p ON HotelSearchResults.HotelID = p.HotelID"Now HotelSearchResults has multiple rows with same [HotelID], i want to get Rows with the Minimum [Price] for earch [HotelID].How can i do this?Where i am making mistake..?
madhivanan
Premature Yak Congratulator
22864 Posts
Posted - 2010-11-11 : 06:37:12
SELECT [ResultID],[HotelSearchResults].[HotelID],[HotelName],p.minPriceFROM [Hotels].[dbo].[HotelSearchResults]INNER JOIN(SELECT HotelID, MIN(Price) AS minPriceFROM HotelSearchResultsGROUP BY HotelID)p ON HotelSearchResults.HotelID = p.HotelID and HotelSearchResults.price=p.minpriceMadhivananFailing to plan is Planning to fail
Ifor
Aged Yak Warrior
700 Posts
Posted - 2010-11-11 : 06:46:17
or:
SELECT ResultID, HotelID, HotelName, PriceFROM( SELECT ResultID, HotelID, HotelName, Price ,RANK() OVER (PARTITION BY HotelID ORDER BY Price) AS Ord FROM HotelSearchResults) DWHERE Ord = 1