I am using the outer join
to blank rows when there is no value but why is it ignoring the line and getting ALL dates
AND t.TradeDate BETWEEN @firstOfMonth AND @maxTradeDate
If I change the AND
to WHERE
it works but I lose the zeroed out rows I need
--DAILY
SELECT
a.[Name] AS RepName,
CONVERT(VARCHAR, a.TradeDate, 107) AS TimePeriod,
ISNULL(SUM(CASE t.CancelCode
WHEN '1' THEN t.Quantity * -1
ELSE t.Quantity
END), 0) AS Quantity,
ISNULL(SUM(CASE t.CancelCode
WHEN '1' THEN t.Principal * -1
ELSE t.Principal
END), 0) AS Principal,
ISNULL(SUM(CASE t.CancelCode
WHEN '1' THEN t.TradeConcession * -1
ELSE t.TradeConcession
END), 0) AS Comm,
1 AS TheOrder
FROM
dayreps a
LEFT JOIN
Trades t ON a.TradeDate = t.TradeDate
AND a.RepID = t.RepID
AND t.TradeDate BETWEEN @firstOfMonth AND @maxTradeDate
GROUP BY
a.[Name], a.TradeDate