Solved

sql select statement for 2 differ tables with unique jobnumber

Posted on 2013-01-14
3
402 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
  • 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

747 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

12 Experts available now in Live!

Get 1:1 Help Now