Solved

How to incorporate a join with current sql query syntax

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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 …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 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

18 Experts available now in Live!

Get 1:1 Help Now