Adding Columns in SQL SELECT
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_Perceptio开发者_StackOverflownist 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:
select *, a.GrossSales - a.Credits as NetSales
from (
--your query here
) a
If You don't want to nest queries and You want to have only one SELECT, you must add another column to your column list (note that it is actually copy & paste of GrossSales and Credits columns with substraction sign between them):
(
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)
) -
(
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 Net
However I would prefer nested queries as RedFilter suggested.
Just a sidenote, unrelated to your question (comment space is too small).
Your two FULL JOIN
are cancelled as the queries have conditions in the WHERE
clause that remove any NULL
produced by the OUTER
joins.
This is actually an INNER JOIN
:
(
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
and this is a RIGHT JOIN
:
FROM T_Perceptionist p
FULL JOIN T_PerceptionistHours h
ON p.PerceptionistID = h.PerceptionistID
WHERE h.WeekOf = @WeekOf
Normally, you just write 'a - b
'. Here, they are fairly horrid expressions, so you use a 'sub-query in the FROM clause':
SELECT PerceptionistID, BaseCommission, BonusCommission, WeekOf, WorkHours, PTOHours,
HolidayHours, HoursRounded, GrossSales, Credits, (GrossSales - Credits) AS NetSales
FROM (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 AS cc
FULL JOIN T_Call AS c ON cc.CallID = c.CallID
WHERE c.PerceptionistID = p.PerceptionistID
AND cc.CallCreditStatusID NOT IN (17, 18)
AND cc.EnteredOn BETWEEN @WeekOf AND DATEADD(dd, 7, @WeekOf)
) AS Credits
FROM T_Perceptionist p
FULL JOIN T_PerceptionistHours h
ON p.PerceptionistID = h.PerceptionistID
WHERE h.WeekOf = @WeekOf
) AS X33;
If you need aggregates in there too, you supply the appropriate GROUP BY clause too.
Long delay while typing answer.
精彩评论