Solved

How to incorporate a join with current sql query syntax

Posted on 2016-11-15
2
40 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 500 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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

696 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