Solved

Performing SQL Join and Calculation Simultaneously

Posted on 2011-02-24
5
410 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

734 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