473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

guru needed - retrieving multiple row values from a separate table while using Min function

Hi guys, i have a little problem and was wandering if anyone can help.
I just need my query modified a little bit to allow some extra values
in there...

Tables:

offers Table

OfferID (primary)
OStartValidity (date)
OValidity (date)
OComments
LocationID
some insignificant Values
hotels Table

HotelID (primary)
LocationID
HName
Some other insignificant values
locations Table

LocationID (primary)
LDescription
LArea
prices Table

PriceID (primary)
OfferID
Price
DepartFrom (date)
DepartTo (date)

Relationships:
locations (one to many LocationID) hotels
locations (one to many LocationID) offers
offers (one to many OfferID) prices

My goal is to retrieve a set of rows from 'offers' table where certain
criteria meets, and then for each row retrieved pull in the Minimum
price from the 'prices' table (matching the same OfferID in 'offers'
obviously). This is my problem - i want to be able to pull in
DepartFrom and DepartTo fields from 'prices' table as well and i dont
know how.

This is the current query i have been using to retrieve just the
Min(price) from prices (i have simplified it a bit to enable you to
comprehend it easier, you will find some extra tables in there not
mentioned above, just ignore them - the ones above are the most
important ones). This has worked fine for me, but as i said i cannot
just add prices.DepartFrom and prices.DepartTo to it as it doesnt seem
to work.

SELECT DISTINCT offers.OfferID, offers.OStartValidity,
offers.OValidity, Min(prices.Price) AS strPrice

FROM locations INNER JOIN ((offers INNER JOIN (hotels INNER JOIN
accomodation ON hotels.HotelID = accomodation.HotelID) ON
offers.OfferID = accomodation.OfferID) INNER JOIN prices ON
offers.OfferID = prices.OfferID) ON (locations.LocationID =
hotels.LocationID)

WHERE offers.OStartValidity<=#2006/3/21# AND
offers.OValidity>=#2006/3/21# AND prices.DepartTo>=#2006/3/21# AND
(locations.LocationID='Dubai Beaches')

GROUP BY offers.OfferID, offers.OStartValidity, offers.OValidity
ORDER BY MIN(prices.Price);

I think i might be needing to use a subquery but unfortunately that is
too complicated for my little brain, i need help!

Mar 21 '06 #1
4 1393
HK
I'm not real clear what you trying to do here.

Is this what you are trying to achieve?: "Which Offers find valid
matches in the Prices table, taking date and price into consideration?"

You get an offer to a specific location like this:

OfferID 357
OStartValidity 03/21/2006
OValidity 03/21/2006
LocationID 36 (Dubai Beaches)

Now you want to know what the min price is in the price table for Dubai
Beaches, starting 03/21/2006 but ending date unspecified, price being
per day? And your prices have to be entered and updated manually for
specific begin dates? Do they take end dates or length of stay into
consideration?

What is the difference between OstartValidity and OValidity?

How do you expect accurate results if you ignore the DepartFrom date?

Your query will likely be very complicated, including subqueries and/or
DLookup, DMax, DMin, DCount, etc. functions. I don't have any ideas
how yet.

Mar 22 '06 #2
if you have a look at the query in my post it might be a bit easier to
comprehend, its actually quite simple, the system works, it has for the
last 2 years... I just need to pull in some extra fields into the
existing query from a separate table.

Holiday offer is valid from OStartValidity to Ovalidity, so OValidity
is end date.
Each offer has a pricerange, so DepartFrom - DepartTo - Price defined
in 'prices' table.
like so:
offer 234 01/01/06 - 02/02/06 $500
offer 234 03/02/06 - 01/05/06 $600
offer 234 02/05/06 - 01/12/06 $550
offer 235 and so on...

This is what im trying to achieve:

Get offers which havent expired for a specific location from 'offers'
table (this is where OStartValidity is used, only fo determining
whether or not the offer has expired). Easy enough right?

So, now we have a list of valid offers from offers table, we will need
to find the lowest price for each of those offers from 'prices' table.
Logically, you could do an extra SQL query and retrieve a set of dates
and prices for a particular offer from 'prices' based on OfferID
(simple SQL along the lines of SELECT
Min(prices.Price),DepartFrom,DepartTo FROM prices where OfferID=235 and
DepartFrom>today) and then work out what the cheapest price is from
several rows returned - thats what im trying to do .
What im trying to do is integrate it into the first SQL query. I have
already done that above for just the Price itself by using
Min(prices.price) but i also need to pull in the dates for that price.

Does that make it any clearer?

Mar 22 '06 #3
sorry in the post above the SQL is incorrect, i was trying to
illustrate
SELECT * FROM prices WHERE OfferID=235 and DepartFrom>today
which returns a number or rows
then find the minimum price out of these rows returned
i just need that row where the minimum price is, the dates and the
price! thats it!

Mar 22 '06 #4

Papachumba wrote:
sorry in the post above the SQL is incorrect, i was trying to
illustrate
SELECT * FROM prices WHERE OfferID=235 and DepartFrom>today
which returns a number or rows
then find the minimum price out of these rows returned
i just need that row where the minimum price is, the dates and the
price! thats it!


See if this helps:

Create query Q1 = select offerId, min(price) as MinPrice from prices
group by OfferId

Create query Q2 = Select offerId, ........., MinPrice, DepartFrom,
DepartTo
from Offers, Prices, Q1 where Offers.OfferId = Prices.OfferId and
Prices.OfferId = Q1.OfferId and Prices.Price = Q1.Price

Mar 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
3
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax:...
2
by: g_chime | last post by:
I have a large table (~200 columns, ~10 million rows) and I am trying to query by two float rows, say f1 and f2: SELECT... FROM large_table WHERE f1 BETWEEN 10.82 AND 113.998 AND f2 BETWEEN...
1
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry =...
9
by: Graham | last post by:
I have been having some fun learning and using the new Controls and methods in .Net 2.0 which will make my life in the future easier and faster. Specifically the new databinding practises and...
10
by: ads | last post by:
hi, after binding the dropdownlist to a datasource, ive experience this error "Cannot have multiple items selected in a dropdownlist" after using the code:...
11
by: Nemisis | last post by:
Hi everyone, sorry if this post gets really long, i just wanna make sure i fully explain what i am trying to do. I am new to OOP and .net 2.0, so if this is obvious, i am sorry. I have wrote a...
2
by: LazyLarry | last post by:
Hi there, I need serious help. Ive got a project that needs some unique select box function. OK THERE IS A LOT OF Code but If you Crack it youll get the credit. just mail me at...
0
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.