473,470 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

outer join with sum

3 New Member
I need to figure out a query in MS Access to combine two intersecting tables into one, where the intersecting values are summed.

Example:
Table1:
ID value
1 10
2 20
3 30

Table2:
ID value
2 40
3 60
4 80

ResultTable:
ID value
1 10
2 60 ( = 20 + 40)
3 90 ( = 30 + 60)
4 80
Mar 12 '09 #1
9 3408
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi. You're not on the right track if you are looking for a solution using an outer join, as whichever table you take as the 'outer' you will not have a complete set of IDs in the result set.

Instead, consider the use of a Union query to combine the two tables together, then you can sum the value columns in the combined rows from the union query grouped by ID to get the result you need.

I am not posting an SQL solution for this at present, as your question could, it seems to me, relate to a college or university course assignment, and for obvious reasons we cannot provide full-code solutions to such assignment questions. The methodology hints (use a union query feeding into or as part of a group by and sum query) should be enough to get you on the right track with this one for yourself, however.

Welcome to Bytes!

-Stewart
Mar 12 '09 #2
NeoPa
32,556 Recognized Expert Moderator MVP
Interesting question clearly asked.

Can you let us know if that helped you to the solution?
Mar 24 '09 #3
tk2393
3 New Member
Thanks!

In answer to your question, I did post my response, but apparently it either got lost or rejected. I laid out the solution that I came up and asked for a critique. However, I never saw my response posted, and I likewise never received a critique. Do you have any record of my second post?
Mar 24 '09 #4
NeoPa
32,556 Recognized Expert Moderator MVP
I've looked, and cannot find any reference to another post from you. Sorry.

I would say though, that we do often see comments that look very much as if they should be posts, but sent as Reports. I don't recall any such report personally, but there are so many emails to go through that I don't pay too much attention to them if they're obviously not reportings.

Is it possible you have some record of it?
Mar 24 '09 #5
tk2393
3 New Member
Here's how I ended up doing it. I had to divide it into three different queries: one query for unique values in Table1, one query for unique values in Table2, and one query for the combined values. Then I combined all three with unions.
Expand|Select|Wrap|Line Numbers
  1. SELECT * INTO ResultTable
  2. FROM [
  3.  
  4. select ID, VALUE from Table1 where Table1.ID not in (select ID from Table2)
  5.  
  6. union
  7.  
  8. select Table1.ID, Table1.VALUE+Table2.VALUE 
  9. from Table1 inner join Table2 on Table1.ID=Table2.ID
  10.  
  11. union
  12.  
  13. select ID, VALUE from Table2 where Table2.ID not in (select ID from Table1)
  14. ]
Apr 22 '09 #6
NeoPa
32,556 Recognized Expert Moderator MVP
Thanks for posting your solution. We always appreciate that here.

What you need here optimally is a FULL OUTER JOIN. This is not supported in Access however.

A cartesian product of the two tables with a WHERE clause selecting only the values that match or include nulls (drop where both values exist and don't match) would be possible, but I suspect would not perform very well.

A solution might be to use two partial OUTER JOINs (a LEFT & a RIGHT, or Table1 LEFT JOIN Table2 as well as Table2 LEFT JOIN Table1) and UNION the two result sets together. I suspect that this may prove more efficient than what you currently have.

NB. Ensure you do not use the ALL predicate of the UNION clause, otherwise you will get repeats of the same results.
Apr 23 '09 #7
NeoPa
32,556 Recognized Expert Moderator MVP
As an illustration, the following should be worth trying.

Let us know if it works for you :
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. INTO ResultTable
  3. FROM (SELECT T1.ID,
  4.              T1.Value+T2.Value AS [Value]
  5.       FROM   Table1 AS T1 LEFT JOIN Table2 AS T2
  6.         ON   T1.ID=T2.ID
  7.       UNION
  8.       SELECT T2.ID,
  9.              T2.Value+T1.Value AS [Value]
  10.       FROM   Table2 AS T2 LEFT JOIN Table1 AS T1
  11.         ON   T2.ID=T1.ID)
Apr 23 '09 #8
FishVal
2,653 Recognized Expert Specialist
Hi, NeoPa.

I'm sure your query will work fine.
Just T2.Value in first subquery and T1.Value in the second should be replaced with zero in a case they are Null.
Apr 23 '09 #9
NeoPa
32,556 Recognized Expert Moderator MVP
Good spot Fish. That's perfectly true and I overlooked it.

The fixed version is :
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. INTO ResultTable
  3. FROM (SELECT T1.ID,
  4.              T1.Value+Nz(T2.Value,0) AS [Value]
  5.       FROM   Table1 AS T1 LEFT JOIN Table2 AS T2
  6.         ON   T1.ID=T2.ID
  7.       UNION
  8.       SELECT T2.ID,
  9.              T2.Value+Nz(T1.Value,0) AS [Value]
  10.       FROM   Table2 AS T2 LEFT JOIN Table1 AS T1
  11.         ON   T2.ID=T1.ID)
Apr 25 '09 #10

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

Similar topics

2
by: Martin | last post by:
I am now working on SQL Server 2000 having had previous experience on a different database. Both of the OUTER JOIN syntaxes is different from what I am used to and I am finding it slightly...
8
by: Matt | last post by:
Hello I have to tables ar and arb, ar holds articles and a swedish description, arb holds descriptions in other languages. I want to retreive all articles that match a criteria from ar and...
4
by: thilbert | last post by:
All, I have a perplexing problem that I hope someone can help me with. I have the following table struct: Permission ----------------- PermissionId Permission
3
by: Dam | last post by:
Using SqlServer : Query 1 : SELECT def.lID as IdDefinition, TDC_AUneValeur.VALEURDERETOUR as ValeurDeRetour FROM serveur.Data_tblDEFINITIONTABLEDECODES def,...
3
by: Martin | last post by:
Hello everybody, I have the following question. As a join clause on Oracle we use " table1.field1 = table2.field1 (+) " On SQL Server we use " table1.field1 *= table2.field1 " Does DB2...
3
by: deko | last post by:
From what I understand, an Inner Join narrow the rows selected to the table with the least results... and an Outer Join does the opposite... SELECT qryTxToQ1.Q1Total, qryTxToQ2.Q2Total,...
14
by: mike | last post by:
I'm using postgresl 7.3.2 and have a query that executes very slowly. There are 2 tables: Item and LogEvent. ItemID (an int4) is the primary key of Item, and is also a field in LogEvent. Some...
1
by: Andreas Bauer | last post by:
Hi, when I try a left outer join on one table everything works fine: select * from (tourist.users u left outer join tourist.user_extended_info ue on (u.id = ue.id)) But now I need to do...
5
by: Eitan M | last post by:
Hello, How can I do outer join in ms-access database ? Thanks :)
1
by: teneesh | last post by:
Here I have a code for a view that has been created by a developer on my team. I am trying to use the very same code to create a view for a different formid/quesid. But I cannot figure out how this...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.