Solved

SQL Syntax Help - Merging 2 tables

Posted on 2011-02-15
14
327 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now