0
我正在寫一個SQL查詢,但有困難與我的聯接/子查詢。 我有一個任務表,我想加入到另一個表'tbl_Profile_Mortgage' 問題是,任務表有多個重複的clientID的,所以加入他們帶回所有包括重複。我已經修改它只看客戶端ID的'taskid = originatorId' 這工作正常,但我想創建另一個連接來顯示我的任務表中的列,但因爲我已經完成任務表中的連接一個子查詢我不能引用任務列。希望這是有道理的。 我已經註釋掉了我想要添加的連接以及要顯示的列ID。任何幫助將不勝感激。 感謝SQL子查詢左加入
SELECT
pm.DateCompleted AS DateCompleted,
pm.DateAppRcvd AS DateAppRcvd,
pm.DatePAckaged AS DatePackaged,
pm.DateOffered AS DateOffered,
pm.DateAppIssued AS DateAppIssued,
pm.DatePassed AS DatePassed,
pm.DateCancelled AS DateCancelled,
pm.DateOfEnquiry AS DateOfEnquiry,
pm.Arrangementfee,
pm.Lender,
pm.DateDip,
cf.ClNTNME,
pm.dateDeclined,
pm.Procfeeexp,
pm.fk_ProfileId,
u.FullName AS Advisor, \t \t \t
pm.Source,
pm.AmountRequested,
c.CaseTypeDesc,
cf.ClientId,
--ISD.ISDESC,
pm.AmountRequested,
pl.SiteNameNum,
pl.SiteAddr1,
pl.SiteAddr2,
pl.SiteTown,
pl.SiteCounty,
pl.SitePostcode
FROM dbo.tbl_Profile_Mortgage AS pm
\t LEFT JOIN dbo.tbl_ClientFile AS cf
\t \t ON cf.ClientID = pm.ClientId
\t LEFT JOIN tbl_Profile_ProjectLand AS pl
\t \t ON pl.FK_ProfileId = pm.FK_ProfileId
\t LEFT JOIN tbl_User AS u
\t \t ON u.UserId \t = pm.UpdatedBy
\t LEFT JOIN dbo.tbl_CaseTypes AS c
\t \t ON c.CaseTypeId = pm.CaseType
\t LEFT JOIN
\t \t (
\t \t SELECT Distinct ClientId
\t \t \t FROM Tasks
\t \t \t WHERE TaskId = OriginatorId
\t \t \t )t
\t \t ON (t.clientid = pm.ClientId)
\t --LEFT JOIN dbo.tbl_DDInitialSource AS ISD
\t -- \t ON t.EnquirySource = ISD.ISId
的依賴ClientID和EnquirySource之間的任務子數據關係查詢連接(查詢中的表t)。如果EnquirySource對每個重複的ClientID實例都是相同的,那麼只需將任何合理的聚合添加到子查詢中即可。例如。 SELECT區分ClientId,MAX(EnquirySource)FROM任務。但是,如果它們不一樣,那麼您需要確定如何最終得到一個「答案」(MIN,MAX,MEAN等)。 – 2014-10-22 10:27:28