Hello everyone. I have a SQL SELECT statement that is working great, but is returning multiple rows in one of my Left Outer Joins. As this table is on the right of the Left Outer Join, I thought it would only return the single row... It's treating this like a Right Outer Join, duplicating the data in the other tables as well. This only occurs when the Table in the left outer join has more than one row returned.
I have been trying to figure this out myself, trying to use DISTINCT, TOP, and MAX(ID), but nothing I do seems to solve this problem.
Does anyone have any suggestions for me here? I really appreciate the help. :(
Here is my SQL Statement. Not all of it pertains to the LEFT OUTER JOIN Table that is causing the issue, but I wanted to post it all so that it was complete.
SELECT Jobs.ID, Jobs.CustomerID AS [CustomerID], COALESCE(NULLIF(Customers.Company,'') + ' - ','') + Customers.LName + ', ' + Customers.FName AS [Name], MAX(BillingBid.ID) AS [BidID], BillingBid.DateCreated AS [Bid Date], SUM(Billing.Amount), FROM Jobs LEFT OUTER JOIN Customers ON Customers.ID = Jobs.CustomerID LEFT OUTER JOIN Billing AS BillingBid ON Jobs.ID = BillingBid.JobID AND BillingBid.BillingType = 'Bid' LEFT OUTER JOIN Billing ON Billing.JobID = Jobs.ID AND Billing.BillingType = 'Invoice' AND Billing.InvoiceCanceled = 'No' WHERE Jobs.JobStatus = 'Active' GROUP BY Jobs.ID, Jobs.CustomerID, Customers.Company, Customers.LName, Customers.FName, BillingBid.DateCreated, Jobs.JobStatus ORDER BY Name Asc
The BillingBid result is the one that is causing the problem. I just have no idea why. The MAX(ID) I thought should cause it to return only the MAX ID in the result, but it is returning them all...
I really appreciate everyone's help. I just haven't had any luck with any of the changes I have tried and it's driving me nuts.
Thanks for all of your help.