×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

SubQueries

SubQueries

SubQueries

(OP)
I need to return a value from a subquery where a field in the main query is joining on a field in the subquery.

Main Query has Loan#, DocTypeName, CreatedDate
SubQuery has Loan#, CancelDate
I need results to return Loan#, CreatedDate, DocTypeName from main query and CancelDate from SubQuery where Loan# matches and the subquery is based off of more than one table already

Here is my subquery:
SELECT LEFT(Loan.sLNm, 6) AS Loan#, Status.sCanceledD AS [CancelInfo]
FROM Agents AS Agents INNER JOIN
Originating_Company AS Originating_Company ON Agents.sPmlBrokerId = Originating_Company.PmlBrokerId RIGHT OUTER JOIN
Loan AS Loan ON Agents.sLId = Loan.sLId LEFT OUTER JOIN
Status AS Status ON Loan.sLId = Status.sLId
WHERE (Status.sCanceledD IS NOT NULL) AND (Loan.sLpTemplateNm = 'Easy Close') AND (Status.sStatusT IN (9, 10, 49))

Here is my Query:
SELECT Loan.sLNm AS Loan#, EDoc.DocTypeName, EDoc.CreatedDate
FROM
Loan AS Loan_1 INNER JOIN
Status AS Status_1 ON Loan_1.sLId = Status_1.sLId RIGHT OUTER JOIN
Agents AS Agents INNER JOIN
Originating_Company AS Originating_Company ON Agents.sPmlBrokerId = Originating_Company.PmlBrokerId RIGHT OUTER JOIN
Loan AS Loan ON Agents.sLId = Loan.sLId LEFT OUTER JOIN
Status AS Status ON Loan.sLId = Status.sLId LEFT OUTER JOIN
EDoc AS EDoc ON Loan.sLId = EDoc.sLId ON LEFT(Loan_1.sLNm, 6) = Loan.sLNm
WHERE (Loan_1.sLpTemplateNm = 'Capital Access') AND (Loan.sLienPosT = 0) AND (Loan_1.sLienPosT = 1) AND (NOT (Status_1.sStatusT IN (9, 10))) AND (EDoc.IsValid = 1) AND (EDoc.DocTypeName LIKE 'sub%') AND
(Originating_Company.Name NOT LIKE 'test%') AND (NOT (Status.sStatusT IN (9, 49))

RE: SubQueries

You could try this....

CODE

With Canceled As
(
	SELECT	LEFT(Loan.sLNm, 6) AS Loan#, 
			Status.sCanceledD AS [CancelInfo]
	FROM	Agents AS Agents 
			INNER JOIN Originating_Company AS Originating_Company 
				ON Agents.sPmlBrokerId = Originating_Company.PmlBrokerId 
			RIGHT OUTER JOIN Loan AS Loan 
				ON Agents.sLId = Loan.sLId 
			LEFT OUTER JOIN Status AS Status 
				ON Loan.sLId = Status.sLId
	WHERE	(Status.sCanceledD IS NOT NULL) 
			AND (Loan.sLpTemplateNm = 'Easy Close') 
			AND (Status.sStatusT IN (9, 10, 49))
)
SELECT	Loan.sLNm AS Loan#, 
		EDoc.DocTypeName, 
		EDoc.CreatedDate
FROM	Loan AS Loan_1 
		INNER JOIN Status AS Status_1 
			ON Loan_1.sLId = Status_1.sLId 
		RIGHT OUTER JOIN Agents AS Agents 
		INNER JOIN Originating_Company AS Originating_Company 
			ON Agents.sPmlBrokerId = Originating_Company.PmlBrokerId 
		RIGHT OUTER JOIN Loan AS Loan 
			ON Agents.sLId = Loan.sLId 
		LEFT OUTER JOIN Status AS Status 
			ON Loan.sLId = Status.sLId 
		LEFT OUTER JOIN EDoc AS EDoc 
			ON Loan.sLId = EDoc.sLId 
			ON LEFT(Loan_1.sLNm, 6) = Loan.sLNm
		Left Join Canceled
			On Loan.sLNm = Canceled.Loan#
WHERE	(Loan_1.sLpTemplateNm = 'Capital Access') 
		AND (Loan.sLienPosT = 0) 
		AND (Loan_1.sLienPosT = 1) 
		AND (NOT (Status_1.sStatusT IN (9, 10))) 
		AND (EDoc.IsValid = 1) 
		AND (EDoc.DocTypeName LIKE 'sub%') 
		AND (Originating_Company.Name NOT LIKE 'test%') 
		AND (NOT (Status.sStatusT IN (9, 49))) 

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close