Hi everyone. I'm sorry to have to post on here about this, but I can't seem to figure out why this would be doing this.
I have the following SQL SELECT, which works perfectly.
SELECT Jobs.ID, Jobs.CustomerID AS [CustomerID], Jobs.ReferralID AS [ReferralID], Jobs.ReferralType, Jobs.AgentID AS [AgentID], Jobs.AdjusterID AS [AdjusterID], Jobs.EstimatorID AS [EstimatorID], Jobs.ManagerID AS [ManagerID], Jobs.JobType, COALESCE(NULLIF(Customers.Company,'') + ' - ','') + Customers.LName + ', ' + Customers.FName AS [Name], Jobs.InsuranceCompany, Jobs.DateReceived,
MAX(BillingBid.DateCreated) AS [Bid Date],
SUM(Billing.Amount) AS [Amount],
StaffEstimator.FName + ' ' + StaffEstimator.LName AS [EstimatorName], StaffManager.FName + ' ' + StaffManager.LName AS [ManagerName],
Jobs.JobStatus
FROM Jobs LEFT OUTER JOIN Customers ON Customers.ID = Jobs.CustomerID
LEFT OUTER JOIN Staff AS StaffEstimator ON StaffEstimator.ID = Jobs.EstimatorID
LEFT OUTER JOIN Staff AS StaffManager ON StaffManager.ID = Jobs.ManagerID
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' AND (Jobs.DatePended IS NULL OR Jobs.DatePended <= '9/27/2014')
GROUP BY Jobs.ID, Jobs.CustomerID, Jobs.ReferralID, Jobs.ReferralType, Jobs.AgentID, Jobs.AdjusterID, Jobs.EstimatorID, Jobs.ManagerID, Jobs.JobType, Customers.Company, Customers.LName, Customers.FName, Jobs.InsuranceCompany, Jobs.DateReceived, StaffEstimator.FName, StaffEstimator.LName, StaffManager.FName, StaffManager.LName, Jobs.JobStatus
ORDER BY Name Asc
Now, I have a table for Communications, and want to add to this SQL Select, the Max Date from the Communications Table that relates to this Customer...
So I added that to the SQL Select like this:
SELECT Jobs.ID, Jobs.CustomerID AS [CustomerID], Jobs.ReferralID AS [ReferralID], Jobs.ReferralType, Jobs.AgentID AS [AgentID], Jobs.AdjusterID AS [AdjusterID], Jobs.EstimatorID AS [EstimatorID], Jobs.ManagerID AS [ManagerID], Jobs.JobType, COALESCE(NULLIF(Customers.Company,'') + ' - ','') + Customers.LName + ', ' + Customers.FName AS [Name], Jobs.InsuranceCompany, Jobs.DateReceived,
MAX(BillingBid.DateCreated) AS [Bid Date],
SUM(Billing.Amount) AS [Amount],
StaffEstimator.FName + ' ' + StaffEstimator.LName AS [EstimatorName], StaffManager.FName + ' ' + StaffManager.LName AS [ManagerName],MAX(Communications.DateCreated) AS [Last Contact],
Jobs.JobStatus
FROM Jobs LEFT OUTER JOIN Customers ON Customers.ID = Jobs.CustomerID
LEFT OUTER JOIN Staff AS StaffEstimator ON StaffEstimator.ID = Jobs.EstimatorID
LEFT OUTER JOIN Staff AS StaffManager ON StaffManager.ID = Jobs.ManagerID
LEFT OUTER JOIN Communications ON Jobs.CustomerID = Communications.ContactID AND Communications.ContactType = 'Customers'
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' AND (Jobs.DatePended IS NULL OR Jobs.DatePended <= '9/27/2014')
GROUP BY Jobs.ID, Jobs.CustomerID, Jobs.ReferralID, Jobs.ReferralType, Jobs.AgentID, Jobs.AdjusterID, Jobs.EstimatorID, Jobs.ManagerID, Jobs.JobType, Customers.Company, Customers.LName, Customers.FName, Jobs.InsuranceCompany, Jobs.DateReceived, Communications.DateCreated, StaffEstimator.FName, StaffEstimator.LName, StaffManager.FName, StaffManager.LName, Jobs.JobStatus
ORDER BY Name Asc
Unfortunately, while I am asking for the MAX Communication that matches the Left Join, it is returning ALL of the Communications that match the Left Join...
Does anyone have any idea why this might be? No matter what I do I get the same result and I don't see anything wrong with the SQL Select... I'm sure I'm missing something stupid...
Thanks for your help,
-Matt-