Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 378
  • Last Modified:

SQL Syntax Help - Merging 2 tables

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
rito1
Asked:
rito1
  • 8
  • 5
1 Solution
 
HainKurtSr. System AnalystCommented:
try

select *
from table1 t1 full join on table2 t2 on t1.regid=t2.regeid
0
 
HainKurtSr. System AnalystCommented:
oops a tyoe above, should read as:

select *
from table1 t1 full join table2 t2 on t1.regid=t2.regeid
0
 
rito1Author Commented:
Thanks HainKurt. I am just trying it now.

Rit
0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
HainKurtSr. System AnalystCommented:
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
 
HainKurtSr. System AnalystCommented:
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
 
rito1Author Commented:
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
 
HainKurtSr. System AnalystCommented:
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
 
LowfatspreadCommented:
where does the sql server come into the process then?

please remoive the topic area if it doesn't apply
0
 
rito1Author Commented:
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
 
rito1Author Commented:
HainKurt,

I am just trying the UNION JOIN theory now.

Rit
0
 
HainKurtSr. System AnalystCommented:
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
 
HainKurtSr. System AnalystCommented:
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
 
rito1Author Commented:
Brilliant. Thanks for your help.

Rit
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now