?
Solved

How to create a complex join

Posted on 2007-12-03
14
Medium Priority
?
236 Views
Last Modified: 2010-04-21
I would like to create a summary table "tblA" based on records in two source tables "tblB" and "tblC". both of these source tables have two index fields - customer account and custoemr bank account. A record in "tblB" maynot have a corresponding record in "tblC" and vice versa. but "tblA" must show sum of all records between tableB and TableC.
0
Comment
Question by:Bartley1969
[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
  • 5
  • 5
  • 2
  • +1
14 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20395612
corresponding... with a "match" on both fields or only 1 of the fields?
anyhow, can you show sample data of the 2 fields and the required output... to avoid any misunderstandings...
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20395648
Hope this will help you
select * into tableA from tableb b join tablec c where b.customeraccount=c.customeraccount and b.bankaccount = c.customeraccount.
0
 
LVL 16

Expert Comment

by:Rick_Rickards
ID: 20395656
This is a 2 step process.  First Create a UNION query to pull all the data togeter.  For this example let's just call it qryInfo

1st Query's Name: qryInfo

SELECT CustomerAccount, CustomerBankAccount FROM tblB;
UNION ALL
SELECT CustomerAccount, CustomerBankAccount FROM tblC;

2nd Query to INSERT data from qryInfo into tblA

INSERT INTO tblA ( CustomerAccount, CustomerBankAccount )
SELECT qryInfo.CustomerAccount, qryInfo.CustomerBankAccount
FROM qryInfo;

Execute the 2nd query and tblA will have everything from tblB and tblC combined.
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20395657
Sorry like this.
select * into tableA from tableb b join tablec c on b.customeraccount=c.customeraccount and b.bankaccount = c.customeraccount
0
 

Author Comment

by:Bartley1969
ID: 20395683
match on both feilds...
attached is sample of contents of tableB and TableC and desired results to show in TableA


TableB		
Customer Acc	Cust bank account	txdet
079	0000	120
078	0001	1200
		
TableC		
Customer Acc	Cust bank account	txdet
079	0000	12
080	0001	12000
		
		
TableA		
079	0000	132
078	0001	1200
080	0001	12000

Open in new window

0
 

Author Comment

by:Bartley1969
ID: 20395687
I forgot to mention, I would like to create a maketable query in access to do this....
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20395703
if both tableB and tableC do NOT have duplicated on the customer/cust bank account:
select coalesce(b.[customer acc], c.[customer acc]) as [customer acc]
, coalesce( b.[cust bank account], c.[cust bank account]) as [cust bank account]
, coalesce( b.txdet , 0 ) + coalesce( c.txdet , 0) as total_txdet
FROM TableC c
FULL OUTER JOIN TableB b
  on c.[Customer Acc] = b.[Customer Acc]
 and c.[cust bank account] = b.[cust bank account]

Open in new window

0
 
LVL 16

Expert Comment

by:Rick_Rickards
ID: 20395727
This is a 2 step process.  First Create a UNION query to pull all the data togeter.  For this example let's just call it qryInfo

1st Query's Name: qryInfo

SELECT Customer, Acc, [Cust bank account] FROM tblB;
UNION ALL
SELECT Customer, Acc, [Cust bank account] FROM tblC;

2nd Query to INSERT data from qryInfo into tblA (If tblA already exists)

INSERT INTO tblA (Customer, Acc,  [Cust bank account])
SELECT qryInfo.Customer, qryInfo.Acc, qryInfo.[Cust bank account]
FROM qryInfo;

OR...

2nd Query to create tblA if it does not already exist (Make Table Query)

Customer, Acc, [Cust bank account]
SELECT qryInfo.Customer,  qryInfo.CustomerAccount, qryInfo.[Cust bank account] INTO tblA
FROM qryInfo;


Execute the 2nd query and tblA will have everything from tblB and tblC combined.
0
 
LVL 16

Accepted Solution

by:
Rick_Rickards earned 2000 total points
ID: 20395734
Sorry, missed the last field in your example...

This is a 2 step process.  First Create a UNION query to pull all the data togeter.  For this example let's just call it qryInfo

1st Query's Name: qryInfo

SELECT Customer, Acc, [Cust bank account], txdet FROM tblB;
UNION ALL
SELECT Customer, Acc, [Cust bank account], txdet FROM tblC;

2nd Query to INSERT data from qryInfo into tblA (If tblA already exists)

INSERT INTO tblA (Customer, Acc,  [Cust bank account], txdet)
SELECT qryInfo.Customer, qryInfo.Acc, qryInfo.[Cust bank account], qryInfo.txdet
FROM qryInfo;

OR...

2nd Query to create tblA if it does not already exist (Make Table Query)

Customer, Acc, [Cust bank account]
SELECT qryInfo.Customer,  qryInfo.CustomerAccount, qryInfo.[Cust bank account], qryInfo.txdet INTO tblA
FROM qryInfo;

Execute the 2nd query and tblA will have everything from tblB and tblC combined.
0
 

Author Comment

by:Bartley1969
ID: 20395783
Rick, I'm following your instructions, many thanks...
0
 
LVL 16

Expert Comment

by:Rick_Rickards
ID: 20395848
You're most welcome.

Rick
0
 

Author Comment

by:Bartley1969
ID: 20396142
Rick,
I've created the union query but it returned each record and not the summary of each unqie record, for example customer acc 079 with bank account 0000 has a record in TableB and TableC, this shows up as two record in the union as aopposed to a single record with the sum of the two records. Any ideas?
0
 
LVL 16

Expert Comment

by:Rick_Rickards
ID: 20396256
Yes, the original query was designed to display all data from both tables B and C even if an identical record existed in each of the two tables.  If you wanted duplicate records to be excluded or rather displayed only once then your UNION query would look like this...

SELECT Customer, Acc, [Cust bank account], txdet FROM tblB;
UNION
SELECT Customer, Acc, [Cust bank account], txdet FROM tblC;

The difference is that UNION ALL shows everything but UNION by itself will remove any duplication between the two tables and display it only once.

Rick
0
 

Author Closing Comment

by:Bartley1969
ID: 31412324
dsicovered that using * to select all fields with UNION requires that all fields in both tables are in the same roder and have same heading spelling (logically!)
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

777 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