This is for a search and it is bringing back too many counts for numVerification0. The INNER JOIN must be in the Case for this to work, not like it is in the other sproc.SELECT a.ImportAlertID, c.Name, a.ProcessDate, CASE WHEN (@type= 4 THEN (SELECT COUNT(*) FROM invoicenotadvanced ina INNER JOIN ImportAlertTrackingNumbers itn ON a.ImportAlertID = itn.ImportAlertID WHERE VerificationStatus = 0 AND ina.ImportAlertID = itn.InvImportAlertID)END AS numVerification0FROM ImportAlert aINNER JOIN ImportType b ON a.ImportTypeID = b.ImportTypeIDINNER JOIN Client c ON a.ClientID = c.pk_id
There are a few more selects and Case statement but this is the short version.There's another page that uses a different sproc and it works:IF @type = 4BEGINSELECT DISTINCT a.ImportAlertID, c.Name, a.ProcessDate,(SELECT COUNT(*) FROM invoicenotadvanced WHERE VerificationStatus = 0 AND ImportAlertID = itn.InvImportAlertID) AS numVerification0,FROM ImportAlert aINNER JOIN ImportType b ON a.ImportTypeID = b.ImportTypeIDINNER JOIN Client c ON a.ClientID = c.pk_idINNER JOIN ImportAlertTrackingNumbers itn ON a.ImportAlertID = itn.ImportAlertIDINNER JOIN invoicenotadvanced inv ON inv.ImportAlertID = itn.InvImportAlertID
Any suggestions?Thanks,Zath