Solved

Need help with SQL Query and joins

Posted on 2008-06-18
1
408 Views
Last Modified: 2012-08-14
Greetings,

I am doing a query that should pull data from 2 related tables.  The relationship is based on both the Job-ID and System-ID being equal.  What I am getting with the below query is 4 results, when I run the query joined only to 1 table at a time I get back 1 result from the MatlUsageData table and 4 from the Production table.  My desired result set would be 5 rows, with the fields from the other table just blank.

Attached is a spreadsheet showing the current results and showing the desired results.  Any help would be appreciated.

Thanks,
John
SELECT     PUB.Job."Job-ID" AS Expr1, PUB.Job."Cust-ID-Bill-to", PUB.Job."Quotation-Amount", PUB.Job."System-ID" AS Expr2, 

                      PUB.MatlUsageData."Cost-Actual", PUB.MatlUsageData."Cost-Estimated", PUB.Production."Job-ID", PUB.Production."Sub-Job-ID", 

                      PUB.Production."System-ID", PUB.Production."Operation-ID", PUB.Production."Dept-ID", PUB.Production."Work-Center-ID", 

                      PUB.Production."Work-Center-Rate"

FROM         { oj { oj PUB.Job LEFT OUTER JOIN

                      PUB.MatlUsageData ON PUB.Job."Job-ID" = PUB.MatlUsageData."Job-ID" AND 

                      PUB.Job."System-ID" = PUB.MatlUsageData."System-ID" } LEFT OUTER JOIN

                      PUB.Production ON PUB.Job."Job-ID" = PUB.Production."Job-ID" AND PUB.Job."System-ID" = PUB.Production."System-ID" }

WHERE     (PUB.Job."Job-ID" = '280892') AND (PUB.Job."System-ID" = '2')

Open in new window

expertsexchangequeryresults.xls
0
Comment
Question by:rugby148
1 Comment
 
LVL 9

Accepted Solution

by:
Tone' Shelby earned 500 total points
ID: 21818186
Hi
Usually if they are of equal match, an INNER JOIN should do the match. If the idea here is that you are losing one record from your join and you have tried that, I would then try a RIGHT JOIN and if that does not bring back what you need, you may try approaching it from a UNION standpoint if that is an accepted option in your environment.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Creating and Managing Databases with phpMyAdmin in cPanel.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

13 Experts available now in Live!

Get 1:1 Help Now