473,378 Members | 1,446 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,378 software developers and data experts.

Sub Query Problem

I have two tables in an Access database. Table "StockCodeCost" stores the StockCode, Cost, and Date (for when the cost of the StockCode changes). This table essentially keeps track of any price change for the StockCode.

Then I have another table "StockCodeSales". This table stores the StockCode, QTYSold, and DateSold.

I want to create a query that can show me the sum of the costs for each transaction (each sale). This requires looking up the cost of the stock code at the time of the sale.

I have a query that will return the cost of the stock code based on a date parameter that I enter. I have tried to use this as a sub query in another query to link my sales table together, but I am not having any luck.

How do I use the DateSold value from one table to be the parameter for my sub query?
Feb 14 '08 #1
9 1768
FishVal
2,653 Expert 2GB
Hi, MWalker.

You may join the tables in the following manner:
Expand|Select|Wrap|Line Numbers
  1. SELECT StockCodeCost.*, StockCodeSales.* FROM StockCodeCost INNER JOIN StockCodeSales ON StockCodeCost.DateSold = DMax("[Date]", "StockCodeSales", "[Date]=#" & StockCodeSales.DateSold & "#");
  2.  
Regards,
Fish
Feb 14 '08 #2
Hi, MWalker.

You may join the tables in the following manner:
Expand|Select|Wrap|Line Numbers
  1. SELECT StockCodeCost.*, StockCodeSales.* FROM StockCodeCost INNER JOIN StockCodeSales ON StockCodeCost.DateSold = DMax("[Date]", "StockCodeSales", "[Date]=#" & StockCodeSales.DateSold & "#");
  2.  
Regards,
Fish
Thanks for the feedback. I've never used the DMax function before. I don't necessarily want the maximum cost of the part at any point in time. What I want is to be able to say on this DateSold, the cost of the StockCode at that time was X amount of dollars. The problem I have is that dates don't match in the two tables. For example, the cost could have changed on Feb. 1st and then again on Feb. 10th, but we sold some on Feb. 3rd and Feb. 12th. The sale price of these two transactions would be different. I need to be able to determine that on Feb. 3rd, the price was the same as it was on Feb. 1st.

The code below is from the query that correctly finds the cost of the stock code based on a date that I enter (in this case it is 'MyDate')

Expand|Select|Wrap|Line Numbers
  1. SELECT TOP 1 StockCodeCost.StockCode, StockCodeCost.Cost, StockCodeCost.Date
  2. FROM StockCodeCost
  3. WHERE (((StockCodeCost.Date)<=[MyDate]))
  4. ORDER BY StockCodeCost.Date DESC;
I think the solution is somehow using your code with the INNER JOIN along with this code shown here. Any suggestions?
Feb 14 '08 #3
FishVal
2,653 Expert 2GB
Ok. I'm not 100% sure it will work as is because I've not tested this particular code. I will explain its logic (I've changed the code because it was erroneous).

Expand|Select|Wrap|Line Numbers
  1. SELECT StockCodeCost.*, StockCodeSales.* FROM StockCodeCost INNER JOIN StockCodeSales ON StockCodeCost.Date = DMax("[Date]", "StockCodeCost", "[Date]<=#" & StockCodeSales.DateSold & "# AND [StockCode]=" & StockCodeSales.StockCode);
  2.  
When database engine take a combination of records from the tables being joined it checks whether cost change date is the same as maximal from those in [StockCodeCost] which are less or equal to the sell date from [StockCodeSales] and [StockCode] is the same.

Sure it is not the optimal strategy. You may post a sanitized copy of your db with only these two tables containing some records sufficient to design a query. I will look for more efficient solution.

Regards,
Fish
Feb 14 '08 #4
Ok. I'm not 100% sure it will work as is because I've not tested this particular code. I will explain its logic (I've changed the code because it was erroneous).

Expand|Select|Wrap|Line Numbers
  1. SELECT StockCodeCost.*, StockCodeSales.* FROM StockCodeCost INNER JOIN StockCodeSales ON StockCodeCost.Date = DMax("[Date]", "StockCodeCost", "[Date]<=#" & StockCodeSales.DateSold & "# AND [StockCode]=" & StockCodeSales.StockCode);
  2.  
When database engine take a combination of records from the tables being joined it checks whether cost change date is the same as maximal from those in [StockCodeCost] which are less or equal to the sell date from [StockCodeSales] and [StockCode] is the same.

Sure it is not the optimal strategy. You may post a sanitized copy of your db with only these two tables containing some records sufficient to design a query. I will look for more efficient solution.

Regards,
Fish
Please excuse my ignorance, but I don't see how to attach files. The help file says
To attach a file to a new post, simply click the [Browse] button at the bottom of the post composition page, and locate the file that you want to attach from your local hard drive.
I don't see this browse button anywhere.
Feb 14 '08 #5
FishVal
2,653 Expert 2GB
Well. I've made some tests. The following query seems to work though looks ugly.

Expand|Select|Wrap|Line Numbers
  1. SELECT StockCodeSales.*, StockCodeCost.*
  2. FROM StockCodeSales INNER JOIN StockCodeCost ON ([StockCodeSales].StockCode=[StockCodeCost].StockCode) AND ([StockCodeCost].[Date]=DMax("[Date]","StockCodeCost","[Date]<=#" & [StockCodeSales].[DateSold] & "# AND [StockCode]=" & [StockCodeSales].StockCode));
  3.  
Feb 14 '08 #6
FishVal
2,653 Expert 2GB
Please excuse my ignorance, but I don't see how to attach files. The help file says

I don't see this browse button anywhere.
Having made a post, click [edit/delete], then click [Manage attachments].
Feb 14 '08 #7
FishVal
2,653 Expert 2GB
Take a look at pure SQL solution. Seems to work.

Query ([qryPriceLatestChange]) returning a date of latest price change for each cell. [keyStockCodeSaleID] is a primary key of [StockCodeSales].
Expand|Select|Wrap|Line Numbers
  1. SELECT StockCodeSales.keyStockCodeSaleID, Max(StockCodeCost.Date) AS dteLatestChange
  2. FROM StockCodeSales INNER JOIN StockCodeCost ON (StockCodeSales.DateSold>=StockCodeCost.Date) AND (StockCodeSales.StockCode=StockCodeCost.StockCode)
  3. GROUP BY StockCodeSales.keyStockCodeSaleID;
  4.  
The next query joins [StockCodeSales] and [StockCodeCost] with the aid of [qryPriceLatestChange].
Expand|Select|Wrap|Line Numbers
  1. SELECT StockCodeSales.*, StockCodeCost.Cost
  2. FROM (StockCodeSales INNER JOIN qryPriceLatestChange ON StockCodeSales.keyStockCodeSaleID=qryPriceLatestChange.keyStockCodeSaleID) INNER JOIN StockCodeCost
  3. ON (StockCodeCost.Date=qryPriceLatestChange.dteLatestChange) AND (StockCodeSales.StockCode=StockCodeCost.StockCode);
  4.  
Feb 15 '08 #8
Hi FishVal,

I really appreciate all your help in this matter. But I am still not able to get this to work. I am trying to do this inside Access 2003 and it may not have the capabilities to process the SQL that you have created.

To compound matters, I can't find the Edit button that would allow me to attach a file. I've searched the help section, searched the forums, but I can't find anything. It's very frustrating. I am obviously new to this forum, but so far I'm not impressed with the help menu.

There is something in the help menu that says that the administrator can turn these functions on and off. Is it possible that since I am so new that these functions have not been turned on?
Feb 24 '08 #9
FishVal
2,653 Expert 2GB
Hi FishVal,

I really appreciate all your help in this matter. But I am still not able to get this to work. I am trying to do this inside Access 2003 and it may not have the capabilities to process the SQL that you have created.
Hi, MWalker.

Access query editor is not capable to represent all kinds of SQL joins in design view (you should work in SQL view). This however doesn't prevent an SQL expression (in all other ways quite valid) from being executed properly. A good reason to learn SQL. ;)

To compound matters, I can't find the Edit button that would allow me to attach a file. I've searched the help section, searched the forums, but I can't find anything. It's very frustrating. I am obviously new to this forum, but so far I'm not impressed with the help menu.

There is something in the help menu that says that the administrator can turn these functions on and off. Is it possible that since I am so new that these functions have not been turned on?
[Edit/Delete] button expected to be on the right/bottom corner of a post you've made. It will disappear after a certain period (1h or something like it).

I'm going to attach a sample with a queries mentioned in above posts.
Attached Files
File Type: zip MWalker.zip (23.5 KB, 86 views)
Feb 24 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Wescotte | last post by:
Here is a small sample program I wrote in PHP (running off Apache 1.3.31 w/ PHP 5.0.1) to help illustrates problem I'm having. The data base is using DB2 V5R3M0. The client is WinXP machine using...
3
by: Brian Oster | last post by:
After applying security patch MS03-031 (Sql server ver 8.00.818) a query that used to execute in under 2 seconds, now takes over 8 Minutes to complete. Any ideas on what the heck might be going...
1
by: Jeff Blee | last post by:
I hope someone can help me get this graph outputing in proper order. After help from Tom, I got a graph to display output from the previous 12 months and include the average of that output all in...
8
by: Adam Louis | last post by:
I would like help resolving this problem. I'm a novice who's been hired to query a hospital database and extract useful information, available to me only in a dynamically generated, downloadable...
6
by: Martin Lacoste | last post by:
Ok, before I headbutt the computer... don't know why when I add criteria in a query, I get an 'invalid procedure call'. I also don't know why after searching the help in access, the various access...
4
by: Apple | last post by:
1. I want to create an autonumber, my requirement is : 2005/0001 (Year/autonumber), which year & autonumber no. both can auto run. 2. I had create a query by making relation to a table & query,...
11
by: Andy_Khosravi | last post by:
My problem: I'm having trouble with a query taking much too long to run; a query without any criteria evaluating only 650 records takes over 300 seconds to run (over the network. On local drive...
4
by: Konrad Hammerer | last post by:
Hi! I have the following problem: I have a query (a) using another query (b) to get the amount of records of this other query (b), means: select count(MNR) as Number from...
4
by: Stan | last post by:
I am using MS Office Access 2003 (11.5614). My basic question is can I run a query of a query datasheet. I want to use more that one criteria and can not get that query to work. I thought I...
2
by: existential.philosophy | last post by:
This is a new problem for me: I have some queries that open very slowly in design view. My benchmark query takes about 20 minutes to open in design view. That same query takes about 20 minutes...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.