Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Syntax Help - Merging 2 tables

Posted on 2011-02-15
14
Medium Priority
?
374 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
[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
  • 8
  • 5
14 Comments
 
LVL 60

Expert Comment

by:HainKurt
ID: 34900905
try

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

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 60

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 60

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 60

Accepted Solution

by:
HainKurt earned 2000 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 60

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 60

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 60

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
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…
Suggested Courses

604 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