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

x
?
Solved

4 tables, show the IDs in each table on one row

Posted on 2014-01-12
1
Medium Priority
?
263 Views
Last Modified: 2014-01-16
I have 4 tables with 3 common fields

4 tables:
1.      WASP
2.      REGIONALS
3.      SCCM
4.      ACCOUNTS

3 common fields in all tables:
1.      Asset
2.      Office
3.      Owner


I need a SQL query to compare the 4 tables and list the 3 common fields

each row should have one asset value e.g.

example of output
0
Comment
Question by:JCTDD
[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
1 Comment
 
LVL 8

Accepted Solution

by:
Surrano earned 2000 total points
ID: 39775875
If I understand correctly you need a full join on Asset field and list the other two for each table.

select w.asset, w.office, w.name,
  r.asset, r.office, r.name,
  s.asset, s.office, s.name,
  a.asset, a.office, a.name
from wasp w
  full outer join regionals r on (coalesce(w.asset,r.asset)=r.asset)
  full outer join sccm s on (coalesce(w.asset,r.asset,s.asset)=s.asset)
  full outer join accounts a on (coalesce(w.asset,r.asset,s.asset,a.asset)=a.asset)
-- insert where clause here if needed
order by coalesce(w.asset,r.asset,s.asset,a.asset);

Open in new window


or natural join which means that all non-join common fields must be uniquely named, leaving only asset as common name:
select asset, wasp_office, wasp_name,
  reg_office, reg_name,
  sccm_office, sccm_name,
  acc_office, acc_name
from (select asset, office as wasp_office, name as wasp_name from wasp) w
  natural full outer join (select asset, office as reg_office, name as reg_name from regionals) r
  natural full outer join (select asset, office as sccm_office, name as sccm_name from sccm) s
  natural full outer join (select asset, office as acc_office, name as acc_name from accounts) a
-- insert where clause here if needed
order by asset;

Open in new window

0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

610 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