Solved

Need help with SQL Query and joins

Posted on 2008-06-18
1
410 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server syntax question 13 32
SQL Help 27 45
Connection to multiple databases 13 19
delete the first occurence of a duplicate row in sql 5 26
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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…

809 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