Solved

sql select statement for 2 differ tables with unique jobnumber

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

739 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