Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql select statement for 2 differ tables with unique jobnumber

Posted on 2013-01-14
3
Medium Priority
?
453 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 2000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

916 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