Solved

sql select statement for 2 differ tables with unique jobnumber

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

930 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

10 Experts available now in Live!

Get 1:1 Help Now