November 11, 2010 at 2:04 am
My query is
"SELECT [ResultID]
,[HotelSearchResults].[HotelID]
,[HotelName]
,p.minPrice
FROM [Hotels].[dbo].[HotelSearchResults]
INNER JOIN
(SELECT HotelID, MIN(Price) AS minPrice
FROM HotelSearchResults
GROUP 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..?
November 11, 2010 at 2:14 am
It's because you've got more than one ResultID for each hotel. Does it matter which one you display? You can use GROUP BY and show the mininum or maximum value per hotel it that gets you what you require.
John
November 11, 2010 at 2:21 am
i guess you have more than one hotelid in one of the table as your results shows diffrent resultid with same records
November 11, 2010 at 2:21 am
Thanks John
I want to get only one Row per Hotel, a row that has the minimum Price.
I am already using group by but is not working properly.
plz view the attached picture with my last post
November 11, 2010 at 2:26 am
In your sample results, the price is the same where the hotel is the same. But you can still group by the hotel name to show the minimum price - just means you can't display the ResultID unless you join back to the original table.
John
November 11, 2010 at 2:33 am
You probably forgot a JOIN condition on the Price Column
SELECT[ResultID], [HotelSearchResults].[HotelID], [HotelName], P.minPrice
FROM[Hotels].[dbo].[HotelSearchResults]
INNER JOIN(
SELECTHotelID, MIN(Price) AS minPrice
FROMHotelSearchResults
GROUP BY HotelID
) P
ON HotelSearchResults.HotelID = p.HotelID
AND HotelSearchResults.Price = P.minPrice -- You missed this Join
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
November 11, 2010 at 2:37 am
Thanks Kingston Dhasian
Problem solved.
im a stupid.....:hehe:
November 11, 2010 at 2:43 am
Such mistakes happen sometimes. So no problems. Glad i could help you out 🙂
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply