473,573 Members | 2,716 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 1401
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
7374
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 -...
3
13209
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...
2
1582
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
3886
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 =...
9
2761
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...
10
6361
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...
11
2014
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)...
2
1667
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
3373
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...
0
7746
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...
0
7986
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. ...
0
8179
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7736
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8036
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...
0
5258
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...
0
3701
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3699
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.