Solved

Performing SQL Join and Calculation Simultaneously

Posted on 2011-02-24
5
409 Views
Last Modified: 2012-05-11
Hi All.,

I have two tables (temp1 and temp2) in an MS Access Project that contain 3 fields each: Code, Symbol, Quantity.  My goal is to JOIN these tables on Symbol and select information into a new table, called TableResult but AT THE SAME TIME calculate the SUM of the Quantity field from temp1 and temp2.

I can achieve a simple SELCT w/JOIN as such:
DoCmd.RunSQL "SELECT temp1.Code, temp1.Symbol, temp1.Quantity INTO TableResult FROM temp1 LEFT JOIN temp2 ON temp1.Symbol=temp2.Symbol"

but do NOT know how to modify the above statement to SUM temp1.Quantity + temp2.Quantity and then have ONLY the resultant NET QUANTITY in my table, TableResult.

Thanks!
0
Comment
Question by:Tom_wbi
  • 3
  • 2
5 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 34972665
DoCmd.RunSQL "SELECT temp1.Code, temp1.Symbol, temp1.Quantity + temp2.Quantity as Quantity INTO TableResult
                FROM temp1
                LEFT JOIN temp2
                  ON temp1.Symbol=temp2.Symbol"
0
 

Author Comment

by:Tom_wbi
ID: 34973108
Hi Sharath:

I have already tried (and tried again per your suggestion) the following:

DoCmd.RunSQL "SELECT temp1.Code, temp1.Symbol, temp1.Quantity+temp2.Quantity As Quantity INTO TableResult FROM temp1 LEFT JOIN temp2 ON temp1.Symbol=temp2.Symbol"

The resulting Quantity in my TableResult is ALWAYS the Quantity from table temp1 - I can never get the mathematical operation to work. My feeling is that since I am only Selecting From temp1 that the code has no idea what temp2.Quantity is???
0
 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 34973157
No, you are not selecting from temp1. You are selecting the records from record set after joining temp1 and temp2. So temp2.Quantity should be considered. Do you have Quantity as NULL or zero in temp2. Can you check the data? Try with INNER JOIN and see if you are still facing any issue for the common data.
Otherwise post some data from your both tables. will check it at my end.
0
 

Author Comment

by:Tom_wbi
ID: 34973178
I noticed I'm storing Quantiy as  text Value - do I need to do some type of conversion, like Cdbl?
0
 

Author Comment

by:Tom_wbi
ID: 34973206
That was it - my own (I won't sai it!)

Here is the resultant code that worked:

DoCmd.RunSQL "SELECT temp1.Code, temp1.Symbol, Cdbl(temp1.Quantity)+Cdbl(temp2.Quantity) As Quantity INTO TableResult FROM temp1 LEFT JOIN temp2 ON temp1.Symbol=temp2.Symbol"

By doing the Cdbl conversion on temp1.Quantity and temp2.Quantity - the mathematical oepration now works. That was my whole problem all along!!!

Thank you - will award points to your answer above for taking the time to try to help.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question