I have the following SQL sp and I would like to add a column ‘NetSales’ which is simply ‘GrossSales’ – ‘Credits’. Is there a way to do this in the same SELECT statement?
SELECT p.PerceptionistID, p.BaseCommission, p.BonusCommission, h.WeekOf, h.WorkHours, h.PTOHours, h.HolidayHours,
ROUND(h.WorkHours, 0) AS HoursRounded,
(
SELECT COUNT(c.PerceptionistID)
FROM T_Call c
WHERE
c.PerceptionistID = p.PerceptionistID
AND c.OutcomeID = @OutcomeSale
AND EnteredOn BETWEEN @WeekOf AND DATEADD(dd, 7, @WeekOf)
) AS GrossSales,
(
SELECT COUNT (c.PerceptionistID)
FROM T_CallCredit cc
FULL JOIN T_Call c
ON cc.CallID = c.CallID
WHERE
c.PerceptionistID = p.PerceptionistID
AND cc.CallCreditStatusID NOT IN (17, 18) -- 17 - 'Error in Customer Account', 18 - 'Courtesy Credit'
AND cc.EnteredOn BETWEEN @WeekOf AND DATEADD(dd, 7, @WeekOf)
) AS Credits
--------------------------------------------------
-- would like to have something like the following
SUM(GrossSales - Credits) AS NetSales
FROM T_Perceptionist p
FULL JOIN T_PerceptionistHours h
ON p.PerceptionistID = h.PerceptionistID
WHERE h.WeekOf = @WeekOf
TIA, Brian
Wrap the whole query with another like this: