Ok, really need some input on this one.It's driving me crazy!!!Here is the proc:SELECT DISTINCT a.ImportAlertID, a.ImportTypeID, b.ImportType, c.Name, inv.sumInvoices, inv.numInvoices, a.FundingRuleChecked, (SELECT COUNT(*) FROM invoicenotadvanced i INNER JOIN ImportAlertAgings iaa ON iaa.ImportAlertID = i.ImportAlertID WHERE Closed = 0 AND iaa.ImportAlertID = i.ImportAlertID) AS openInvoices, a.CompleteStatus, '' AS sumCurrentInvoicesFROM ImportAlert aINNER JOIN ImportType b ON a.ImportTypeID = b.ImportTypeIDINNER JOIN Client c ON a.ClientID = c.pk_idINNER JOIN (SELECT ImportAlertID, COUNT(*) AS numInvoices, SUM(InvAmount) AS sumInvoices FROM dbo.ImportAging GROUP BY ImportAlertID) inv ON a.ImportAlertID = inv.ImportAlertIDWHERE a.CompleteStatus = 0 AND a.ImportTypeID = @typeAND DATEDIFF(day, ProcessDate, GETDATE()) <= (SELECT Days FROM ImportMaxDays WHERE ImportTypeID = @type)ORDER BY c.Name
The problem is the count (in bold).I have a table for a reference, ImportAlertAgings.Problem is, it's counting them all and for each row returned,the count is the same.The count should be for each distinct importalertid in ImportAlertAgings and the other table.Suggestions?Thanks,Zath