Solved

SQL Syntax Help - Merging 2 tables

Posted on 2011-02-15
14
347 Views
Last Modified: 2012-05-11
Hi All,

Can you help me with some syntax..

I have the following 2 tables which are clearly different other than RegID and RegEID which in some instances they store the same values:

Table1

RegID   Title    Unit
1           Test    2
2            Prep   1
3            Start   1

Table2
RegEID     Capacity    Date
1               1.6             02/01/2009
3                2.2            10/12/2010
5                4.0             01/02/2011

What I need to achieve is I need to return all records from both tables whether RegID and RegEID match or not. If they do match then these records need all show on the same row like so:

RegID/RegEID     Title          Unit             Capacity         Date
1                         Test          2                 1.6                 02/01/2009
2                         Prep          1                 NULL             NULL
3                         Start          1                 2.2                10/12/2010
5                         NULL        NULL           4.0                 01/02/2011


Can anyone provide any guidance on this?

Many thanks,

Rit
0
Comment
Question by:rito1
  • 8
  • 5
14 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 34900905
try

select *
from table1 t1 full join on table2 t2 on t1.regid=t2.regeid
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34900914
oops a tyoe above, should read as:

select *
from table1 t1 full join table2 t2 on t1.regid=t2.regeid
0
 
LVL 1

Author Comment

by:rito1
ID: 34900923
Thanks HainKurt. I am just trying it now.

Rit
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 51

Expert Comment

by:HainKurt
ID: 34900931
if you want only one id


select ISNULL(t1.id,t2.id) as RegID, t1.*, t2.*
from table1 t1 full join table2 t2 on t1.regid=t2.regeid
order by 1
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34900949
of course id should replace with regid/regeid

select ISNULL(t1.regid,t2.regeid) as RegID, t1.*, t2.*
from table1 t1 full join table2 t2 on t1.regid=t2.regeid
order by 1
0
 
LVL 1

Author Comment

by:rito1
ID: 34900992
select *
from table1 t1 full join table2 t2 on t1.regid=t2.regeid

I currently get syntax error on the FULL keyword. Any ideas?... Just to confirm, I am running this SQL in Access 2007.

Thanks,

Rit
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 34901049
access does not support full join... so we need some unions...

try this

select * from table1 t1 left join table2 t2 on t1.regid=t2.regeid
union
select * from table2 t2 left join table1 t1 on t1.regid=t2.regeid

then we can work on combining the id's into one column...
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34901053
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34901066
where does the sql server come into the process then?

please remoive the topic area if it doesn't apply
0
 
LVL 1

Author Comment

by:rito1
ID: 34901149
Sorry about the inclusion of the SQL Server topic. Yes this is incorrect and should be for Access. I cant seem to remove it.

Rit
0
 
LVL 1

Author Comment

by:rito1
ID: 34901158
HainKurt,

I am just trying the UNION JOIN theory now.

Rit
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34901210
try this one too

select * from table1 t1 left join table2 t2 on t1.regid=t2.regeid
union
select * from table1 t1 right join table2 t2 on t1.regid=t2.regeid WHERE t1.RegID is null


0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34901248
or filtering the cross join if the tables are relatively small

select * from table1 t1, table2 t2 where (t1.RegID=t2.RegEID or t1.RegID is null or t2.RegEID is null)
0
 
LVL 1

Author Closing Comment

by:rito1
ID: 34905073
Brilliant. Thanks for your help.

Rit
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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Viewers will learn how the fundamental information of how to create a table.

776 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