I'm writing a reporting stored procedure. I want to get the number of non-Acknowledged and non-Invoiced Purchase Orders, with the ability to (optionally) filter on CustomerID
.
What I have below works as expected, but I worry that a) it's slow and b) there's duplication in the CustomerID
portion of the WHERE
clause.
How would you write this stored proc, Stack Overflow?
SELECT
(SELECT COUNT(*)
FROM PurchaseOrder
WHERE AcknowledgmentStatus <> 'Complete'
AND (@CustID = 0 OR CustomerID = @CustID)
) AS NonAckd,
(SELECT COUNT(*)
FROM PurchaseOrder
WHERE InvoiceStatus <> 'Complete'
AND (@CustID = 0 OR CustomerID = @CustID)
) AS NonInvoiced