Solved

sql select statement for 2 differ tables with unique jobnumber

Posted on 2013-01-14
3
442 Views
Last Modified: 2013-01-14
I have 2 tables from 2 DB on the same server that import seperate items.
 DB1 collects Labor for Jobs
DB2 Collects equipment for Jobs

Both tables contain 2 columns ( JobNumber and JobName)

JobNumber in both tables will always have the same JobNumber
Jobname may be not always be exactly the same.

I need to write a SQL query that will display a list of the jobnumbers and a jobname (JobName can come from either table)

Now the table may contain equipment for a job, but no labor, and visa versa.

So the tables may look like this:

db1.LaborTable                                 db2.EquipmentTable
JoNu           Jobname                        JoNu             JobName
20021        Ellis Apartments            20021           Elis Apartments
20022        510 South Street           20022           Drexel warehouse
20023        Sam's warehouse          20024           Mike's studios
20023        Smas Warehouse          20024            Mikes Studios


I need the query to display to look something like this:
20021        Ellis Apartments
20022        Drexel warehouse (Either or)
20023        Sam's warehouse          
20024        Mike's studios

Distinct and inner join are out the window, as distinct will give me (2) 20023
and Inner join will give me no jobs for 20023 and 20024.

Getting them to spell the name of the job the same is out the window too.
0
Comment
Question by:BKennedy2008
[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
  • 2
3 Comments
 
LVL 41

Expert Comment

by:ralmada
ID: 38775881
have you tried with UNION ?

select jonu, jobname from db1.labortable
union
select jonu, jobname from db2.EquipmentTable
0
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
ID: 38775898
sorry, I missed this part:

20022        510 South Street           20022           Drexel warehouse

so how you decide which one is the one needed?

>>20023        Sam's warehouse          20024           Mike's studios
20023        Smas Warehouse          20024            Mikes Studios<<

So if you have spelling issues, there's no way you can fix it with SQL. You will have to do it manually or like below if you don't care which one comes first

select jonu, max(jobname) from (
select jonu, jobname from db1.labortable
union
select jonu, jobname from db2.EquipmentTable
) a
group by jonu
0
 

Author Closing Comment

by:BKennedy2008
ID: 38776029
That did the trick..........Thx...........
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

624 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