Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SubQueries 1

Status
Not open for further replies.

jchewsmith

Technical User
Nov 20, 2006
161
US
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))
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top