Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi friends,

I have 3 tables , Hotel, Room, Price. what I want is i want to take the room with minimum price. I'm searching with city.

Hotel table

HotelId Name TotalRooms Address City
<t> 1 A 3 cc D
2 B 2 xx D

Room

RoomId HotelId RName
1 1 AC
2 1 BC
3 1 CC
4 2 DX
5 2 EX
Price

RoomID Price
1 234
2 200
3 150
4 500
5 210

so while searching with city D
i want the result as

hotelID RoomId RName Price
1 3 cc 150
2 5 EX 210


my query is
SQL
select   min(price) as price ,Pr.RoomId,H.Hotelid,H.Name from hotel H
     join Rooms R on H.HotelId=R.hotelId
     join Prices PR on R.RoomId  = PR.RoomId
 where city='Dubai' and price>0
      group by  PR.RoomId,H.Hotelid,H.Name


its not giving the result i want


Any help is appreciated

Regards

Chinnu
Posted
Comments
Om Prakash Pant 8-Oct-12 8:56am    
also, in the sample data you entered, the city is 'D' and not 'dubai'

Hi Chinnu ...

Try this code block

NOTE :- Replace column names and table name with your existing column and tables names.
SQL
SELECT HotelName,RoomName,Price FROM (
     SELECT H.Name As HotelName,H.TotalRooms,R.RoomName,RP.Price,
     ROW_NUMBER() OVER(PARTITION BY H.HotelId ORDER BY RP.Price) RowNum
             FROM Hotel H
          JOIN ROOMS R ON H.HotelId = R.HotelID
          JOIN PRICES RP ON RP.RoomId = R.RoomId
     WHERE CITY = 'D') AS A
WHERE A.RowNum = 1


Thank you
 
Share this answer
 
Comments
chinnu11 9-Oct-12 3:00am    
its working
I want one more help ,In Price table ie (RP) i have startdate,status means (close,changedRoom), entireproperty which is boolean . I want to take the count of rooms ie present in both room and price table, and startdate should fall in between to and from date , if the count > 0 then message is "Entire"
damodara naidu betha 9-Oct-12 6:24am    
Hi Chinnu.. pls provide the structure and sample data of the Price table.
chinnu11 9-Oct-12 6:44am    
can u give me uR MAILID. Becoz so many table are there not these 3 . If u hav no pblm ;)
damodara naidu betha 9-Oct-12 7:21am    
damodaranaidu.betha@gmail.com , please explain your question clearly..
Try this one
SQL
SELECT h.HotelID, h.Name, pr.RoomID, mn.MinPrice
  FROM Hotel h
  JOIN (SELECT r.HotelID, MIN(p.Price) AS MinPrice
          FROM Room AS r
          JOIN Price AS p
            ON p.RoomID = r.RoomID
         GROUP BY r.HotelID ) AS mn
    ON mn.HotelID = h.HotelID
  JOIN Room AS r
    ON mn.HotelID = r.HotelID AND
       mn.MinPrice = r.Price
 WHERE h.City = 'Dubai' AND pr.MinPrice > 0

Good luck :)
 
Share this answer
 
Comments
Tim Corey 8-Oct-12 8:45am    
The only problem I see here is that this will give you the minimum price per hotel. From the question, it looks like the OP wants the lowest price overall, not just the lowest price for every hotel.
skydger 8-Oct-12 8:54am    
I agree, it is the way I got the question, so lets see what he exactly needed :) Anyway your solution more comprehensive.
Your problem is your GROUP BY. This will give you the minimum price for every group, not overall. Since your groups are room-specific, you are going to get every price. There are a couple things you can do. What I like to do is something simple, like a TOP 1 solution:
SQL
select TOP 1 PR.price ,Pr.RoomId,H.Hotelid,H.Name from hotel H
     join Rooms R on H.HotelId=R.hotelId
     join Prices PR on R.RoomId  = PR.RoomId
where city='Dubai' and price>0
ORDER BY PR.price

That will give you your least-expensive room. You could change it to TOP 3 if you wanted your three least expensive rooms.

However, this won't give you the ties. Instead, it will just give you the first of the ties. So, if you have three rooms that each cost $1, you will only get one of them. If you want all three, you need to go back to a GROUP BY but you need to change it to look like this:
SQL
SELECT outPR.RoomId,outH.Hotelid,outH.Name, t.Price
FROM (
select   min(PR.price) as price from hotel H
     join Rooms R on H.HotelId=R.hotelId
     join Prices PR on R.RoomId  = PR.RoomId
where city='Dubai' and price>0) as t
INNER JOIN Prices outPR ON t.price = outPR.price
INNER JOIN Rooms outR ON outPR.RoomId = outR.RoomId
INNER JOIN Hotel outH ON outR.HotelId = outH.HotelId

That is ugly but what it does is it gets just the Min price. It then does another join including that price. That way any room with that price will be chosen. You could also put the inner query in the WHERE statement and say "WHERE t.price = outR.Price" but the problem with that is that you will be running that for every row. The JOIN will perform better. You could also put the inner query into a temp table. I believe that would perform even better.
 
Share this answer
 
try this
SQL
create table hotel
(Id int,
Name varchar(10),
totalrooms int,
City varchar(5))

create table room
(Roomid int,
hotelid int,
Rname varchar(10))

create table price
(RoomId int,
Price int)

insert into hotel values(1,'A',3,'D')
insert into hotel values(2,'B',2,'D')

insert into room values(1,1,'AC')
insert into room values(2,1,'BC')
insert into room values(3,1,'CC')
insert into room values(4,2,'DX')
insert into room values(5,2,'EX')

insert into price values(1,234)
insert into price values(2,200)
insert into price values(3,150)
insert into price values(4,500)
insert into price values(5,210)

select hotelid,room.roomid,rname,price from room 
inner join hotel h on h.Id=room.hotelid
inner join price p on p.RoomId=room.Roomid
where City='D' and price 
in
(select MIN(price) from price p 
inner join room r on r.Roomid=p.RoomId
inner join hotel h on h.Id=r.hotelid
where City='D'
group by r.hotelid)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900