473,749 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.DepartFr om and prices.DepartTo to it as it doesnt seem
to work.

SELECT DISTINCT offers.OfferID, offers.OStartVa lidity,
offers.OValidit y, Min(prices.Pric e) AS strPrice

FROM locations INNER JOIN ((offers INNER JOIN (hotels INNER JOIN
accomodation ON hotels.HotelID = accomodation.Ho telID) ON
offers.OfferID = accomodation.Of ferID) INNER JOIN prices ON
offers.OfferID = prices.OfferID) ON (locations.Loca tionID =
hotels.Location ID)

WHERE offers.OStartVa lidity<=#2006/3/21# AND
offers.OValidit y>=#2006/3/21# AND prices.DepartTo >=#2006/3/21# AND
(locations.Loca tionID='Dubai Beaches')

GROUP BY offers.OfferID, offers.OStartVa lidity, offers.OValidit y
ORDER BY MIN(prices.Pric e);

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 1410
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.Pric e),DepartFrom,D epartTo FROM prices where OfferID=235 and
DepartFrom>toda y) 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.pric e) 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>toda y
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>toda y
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
7393
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 atmosphere. Good food.", " (Harry Houdini - 03/01/2004)"); INSERT INTO `reviews` VALUES("", "Le Chow Place", "Lunch", "yada yada", " (Herbert Hoover - 03/03/2004)"); INSERT INTO `reviews` VALUES("", "Golden Dragon", "Lunch", "Exquisite.
3
13223
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: Hunter_69. Here is what the form looks like. I have the difficulty of inserting the multiple items selected by the user the first time he visits and uses the screen and then using an UPDATE when he visits later. Model | Original Price | Reduced Price...
2
1590
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 -124.99 AND 23.5; The query is extremely slow; it takes hours.
1
3894
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 = New DirectoryEntry("LDAP://domain") Dim mySearcher As New DirectorySearcher(enTry) Dim resEnt As SearchResult mySearcher.Filter = ("(objectClass=*)") mySearcher.SearchScope = SearchScope.Subtree
9
2776
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 wizards. But, I have found that trying to do something "outside the norm" adds a rather large level of complexity and/or data replication. Background I have been commissioned to create a web-based application for a client. It has a formsaunthentication...
10
6388
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: dropdownlist.items.findbyvalue("value").selected = true I didnt specify any selected item in the dropdownlist during binding to the datasource. I use dropdownlist.clearselection() but still error occurs. I need information on this. Thanks. Ads
11
2031
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 data access layer, which contists of separate servicebases for each object within my database. CompanyDataService (contains CRUD methods) ContactDataService (contains CRUD methods)
2
1674
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 info@malachite.de with your Info you wold like posted My issue: 1. I've got two select boxes, of wich one gets filled with MYSQL Data.. Works great...
0
3393
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 within options. I have everything being dynamically named from the previously dynamically named element. (I hope this makes sense.) I am not able to retrieve any of the dynamically created values. I can view them on the source page but can't pull them...
0
8997
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8833
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9568
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8257
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6079
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.