當我嘗試限制我的結果時,我限制它們太多。子查詢,嵌套選擇,加入,別的東西? - 不確定
Select Distinct Agreements.AgrmntID, Agreements.Status, JobSites.SiteName,
AgreementCharges.TotalSaleAmt, AgreementCharges.PartID,
AgreementSchedules.FormanEmpID, LaborCodeTypes.RepairCode
From Agreements Inner Join
AgreementCharges On Agreements.AgrmntID = AgreementCharges.AgrmntID Inner Join
JobSites On JobSites.CustSiteID = Agreements.CustSiteID Left Outer Join
AgreementSchedules On Agreements.AgrmntID = AgreementSchedules.AgrmntID
Left Outer Join
LaborCodeTypes On LaborCodeTypes.RepairID = AgreementSchedules.RepairID
Where Agreements.Status = 2 And AgreementCharges.PartID = 31 And
LaborCodeTypes.RepairCode = '04 - LS'
如果沒有RepairCode匹配'04 - LS」我仍然需要AgrmntID和TotalSaleAmt。同樣,如果沒有AgreementSchedule,我需要AgrmntID和TotalSaleAmt。
輸出:
AgrmntID Status SiteName TotalSaleAmt PartID FormanEmpID RepairCode
-------------------------------------------------------------------------
2 2 TEST01 $100.00 31 66 04 - LS
2 2 TEST03 $300.00 31 21 04 - LS
預期輸出:
AgrmntID Status SiteName TotalSaleAmt PartID FormanEmpID RepairCode
-------------------------------------------------------------------------
2 2 TEST01 $100.00 31 66 04 - LS
2 2 TEST02 $200.00 31
2 2 TEST03 $300.00 31 21 04 - LS
我看着看着,我現在來這裏尋求幫助。先謝謝你!!
哈哈 - 謝謝...我使用的程序不斷地重新格式化我輸入的內容 - 我會盡量讓它在未來更好!這工作得很好,包括了NULL LaborCodeType,但是跳過了既沒有NULL也沒有'04-LS'的那個。在這些情況下,與LaborCodeTypes.RepairID ='供應商賬單'的AgreementSchedule創建時沒有'04-LS'...有沒有一種方法可以將它們包含在與PartID = 31匹配時? –
stop-stop-stop :)我想我只是失去了你的想法,所以讓我們一步一步來做。爲此,只需在'where'子句中刪除最後2個左邊的連接和條件,並查看結果(沒有2個最後一列)。問問自己 - 這是你想要的嗎?如果是,則添加'left join AgreementSchedules ...'並再次問自己。如果沒有,那麼我們需要了解應該添加哪些條件。 – pkuderov
非常感謝您花時間回答我做了您的建議,並且一切正常,只是省略了一些條目。我編輯了你的答案,希望你能理解並幫助我弄明白這一點! –