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

Query Problems

PhilOfWalton
1,430 Expert 1GB
I am trying to find the value of a particular share an different dates.

Obviously the price changes daily, and the quantity of shares held changes from time to time.

The 3 basic tables are
Expand|Select|Wrap|Line Numbers
  1. Investments
  2.     InvestmentID   'Primary Key Autonumber
  3.     SecurityName   'Name of Investment
Expand|Select|Wrap|Line Numbers
  1. BoughtSold
  2.     BoughtSoldID    Primary Key Autonumber
  3.     InvestmentID
  4.     DateBoughtSold
  5.     Balance
Expand|Select|Wrap|Line Numbers
  1. Valuations
  2.     ValuationID
  3.     InvestmentID
  4.     ValDate
  5.     SharePrice
All 3 tables are joined on InvestmentID

For simplicity I have set InvestmentID to 172 so that results can be examined

This is Query7 (which I will rename it when it is all working)
Expand|Select|Wrap|Line Numbers
  1. SELECT Investments.InvestmentID, 
  2. Investments.SecurityName, 
  3. BoughtSold.Balance, 
  4. BoughtSold.DateBoughtSold, Nz(DMax("DateBoughtSold","BoughtSold","BoughtID < " 
  5. & [BoughtID] & " AND InvestmentID = " 
  6. & [Investments]![InvestmentID]),#1/1/1900#) 
  7. AS PrevDate
  8. FROM Investments INNER JOIN BoughtSold ON Investments.InvestmentID = BoughtSold.InvestmentID
  9. WHERE (((Investments.InvestmentID)=172))
  10. ORDER BY BoughtSold.DateBoughtSold;
This query gives me the balance when shares were bought or sold and also the date of the previous purchase /sale, so that the balance of shares held at each date that the share price was entered was known.
In the case of the first purchace, I have arbitrarily set the previous date to 01/01/1900.

This works perfectly

This is Query8
Expand|Select|Wrap|Line Numbers
  1. SELECT Query7.*, Valuations.ValDate, 
  2. Valuations.SharePrice, 
  3. [Balance]*[SharePrice]/100 AS Val
  4. FROM Valuations 
  5. LEFT JOIN Query7 
  6. ON Valuations.InvestmentID = Query7.InvestmentID
  7. WHERE (((Valuations.ValDate)<=[DateBoughtSold] 
  8. And (Valuations.ValDate)>[PrevDate]));
The second query is giving the correct results other than it is not "picking up" information after the date of the last purchase /sale because there is no DateBoughtSold or previous date.

I am looking at ways of picking up those final entries. Somehow I feel that Query7 should have an additional line with the Previous Date set to the last date in the table, and an artificial DateBoughtSold set to 31/12/2099 (far in the future)

I would welcome any suggestions.

Phil
Jul 27 '18 #1

✓ answered by PhilOfWalton

OK, not to worry. Had to use a UNION Query to get valuations after the MaxDateBoughtSold.

Seems a bit complex for something that should have been easy.

Phil

1 1144
PhilOfWalton
1,430 Expert 1GB
OK, not to worry. Had to use a UNION Query to get valuations after the MaxDateBoughtSold.

Seems a bit complex for something that should have been easy.

Phil
Jul 27 '18 #2

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

Similar topics

6
by: carverk | last post by:
Hello All I'm in the middle of moving a MS Access DB to a MySql backend. I have figured out about 90% of the problems I have faced, execpt for this one. I have 3 Queries, which pull records...
1
by: Ryan Govostes | last post by:
Eh, I was wondering if anyone could help me with a SQL query problem I'm having. I'm a complete newbie to SQL and MySQL, so any help would be greatly appreciated. I am using PHP to allow users...
4
by: Russell | last post by:
I'm having a fit with a query for a range of dates. The dates are being returned from a view. The table/field that they are being selected from stores them as varchar and that same field also...
1
by: tranvirus | last post by:
Hi, I have these two XML documents that I'm trying to merge, they are identical except by the content they have is in different languages so I want to generate a third document with a new format...
4
by: Greg | last post by:
I am trying to set a perameter for my query but keep getting an error: The Microsoft Jet database engine does not reconize '' as a valid field name or expression. The only difference from other...
3
by: Reginald | last post by:
I am trying to create a query based on 1 of 3 date fields from a table called "T40". the first date is T40RbtDte, 2nd is T40RbtRecDte, 3rd is T40RbtDueDte. I have created a form to alow the user...
2
by: Kristian | last post by:
Hey, I have some problems combining 2 fields in a query. For example: the first field is "1050-04" and the second field is "02". However, when i try to combine these two the zero in the...
4
by: Jean | last post by:
Hi all, I am trying to get this query right, but so far no joy... I have written the following code in the Field box of the QBD grid of a query: firstvalue:IIf(Not (IsNull()),,(IIf(Not...
6
by: jimmy | last post by:
Hi In my application i have a dataSet which has been created by the designer. I have added the following query to one of the tables; ...
2
by: webhead74 | last post by:
Hi, I'm having intermittent problems with queries from my php script to a postgresql database. I have a form where I can enter a search query - for instance a last name. This leads to a...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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,...

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.