?
Solved

How to incorporate a join with current sql query syntax

Posted on 2016-11-15
2
Medium Priority
?
42 Views
Last Modified: 2016-12-28
I have a sql query that works well. I need to add an additional field form tblIR called review (table) BUT it does not always have a record for the field used as the key.

When I add "and f..gateid = a.gateId to current sql query, I lose rows, since it is only bringing back matching rows.

Is there a way to code a join for table a (a.gateid) and table f (f.gateid) without having to recode the sql query so if there is no record, the field for "review" is blank. If there is a hit it says "yes:.  

(or do I have to code into joins for the whole sql query?)
sqlquery.docx
0
Comment
Question by:mahpog
[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 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 2000 total points
ID: 41888890
Personally I would use ansi-92+ syntax and add an OUTER JOIN on "gateId".  Then use a CASE to display 'Yes' when the "gateId" has a match, or "No" otherwise.

SELECT
e.Acronym as Bus
, d.Acronym as BusArea
, a.Gateid
, a.ModifiedDate
, a.Modid
, a.IRID
, a.plandate
, a.gateactualdate
, a.PlanDateChgReason,
, a.PlanDateChgReasonModifiedDate
, CASE WHEN f.gateid IS NOT NULL THEN 'Yes' ELSE 'No' END AS Review
 
FROM tblgate a
		INNER JOIN tblMod b ON a.ModID = b.ModID
		INNER JOIN tblContract c ON b.contractID = c.ContractID
		INNER JOIN tblBusinessArea d ON c.BusinessAreaID = d.BusinessAreaID
		INNER JOIN tblBusiness e ON d.BusinessID = e.BusinessID
		LEFT JOIN review f ON f.gateid = a.gateId
WHERE e.Acronym = 'XXXXXX'
ORDER BY  a.GateId

Open in new window

0
 

Author Closing Comment

by:mahpog
ID: 41889767
Right on point. I guess I should just use new syntax. Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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.​
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

770 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