Google Answers Logo
View Question
 
Q: SQL Query - Finding the least expensive vendor for a particular hotel ( No Answer,   2 Comments )
Question  
Subject: SQL Query - Finding the least expensive vendor for a particular hotel
Category: Computers > Programming
Asked by: _len_-ga
List Price: $20.00
Posted: 22 Jul 2005 17:53 PDT
Expires: 21 Aug 2005 17:53 PDT
Question ID: 546791
I have a database table that stores the cost of a single night's stay
at various hotels, offered by various vendors:

REATE TABLE [dbo].[Rates] (
	[ResortID] [int] NOT NULL ,
	[VendorID] [int] NOT NULL ,
	[DateofStay] [datetime] NOT NULL ,
	[Cost] [float] NOT NULL ,
	[DateSearched] [datetime] NOT NULL ,
	[RoomType] [int] NOT NULL 
)

Each vendor can supply rooms for multiple hotels.  For example if
Expedia is a vendor, they can offer hotel rooms for Holiday Inn, Best
Western, and so on.  Other vendors may also offer hotel rooms for
these hotels.  Vendors do not have to offer every hotel, however.

Vendor's don't have to offer a room every night, either.  So Expedia
can offer a room at the Holiday Inn on August 1, 2, 3, 4, and 6, but
not August 5 (because it may be sold out).

My question is how I would write an SQL query (using MySQL 3.23.58)
that finds the least expensive vendor for every hotel room in the
table?  So if ten different vendors offer rooms at the Holiday Inn,
and they've all got availability for the dates I want, how do I find
which vendor is the least expensive?
Answer  
There is no answer at this time.

Comments  
Subject: Re: SQL Query - Finding the least expensive vendor for a particular hotel
From: pmmbala1976-ga on 23 Jul 2005 23:43 PDT
 
Hi

Try this type of sample sql query. i am not sure about Mysql. just an idea. 

SELECT vendorid from table1 where resortid=xxx and cost=(select
min(cost)  from table1 where resortid=xxx)

xxx - resortid

thanks
bala
Subject: Re: SQL Query - Finding the least expensive vendor for a particular hotel
From: manuka-ga on 25 Jul 2005 01:06 PDT
 
Add "and DateOfStay = yyy" into the where clause of the subquery, with
yyy being the requested date of stay.

More realistically you'd probably want to be able to specify a stay of
several nights and purchase the whole block in a single transaction.
It should go something like this: (NB I'm only familiar with Oracle
SQL, so some translation may be required)

select vendorid, cost from
    (select vendorid, sum(cost) as cost from
        (select vendorid, dateofstay, cost from Rates
        where resortid = xxxx 
        and dateofstay >= yyyy and dateofstay <= zzzz
        and roomtype = wwww) Sub1
    group by vendorid 
    having count(*) = (zzzz - yyyy + 1)) Sub2
where cost = (select min(cost) from Sub2)

xxxx = desired resort ID
yyyy = start of stay
zzzz = end of stay
wwww = desired room type
Note: (zzzz - yyyy) + 1 = number of days staying

Will not consider vendors who do not have the desired resort available
for the full length of stay. Assumes there is only (at most) one
record in the DB with a given resort ID, vendor ID, date of stay and
room type. This may not be so, given that you have a DateSearched
field in the table.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy