Solved

Performing SQL Join and Calculation Simultaneously

Posted on 2011-02-24
5
412 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Suggested Courses

630 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