Примерно так:
DECLARE @LastDayOfLastMonth DATETIME = EOMONTH(@FirstDayOfLastMonth)
--SELECT SUM(Debit)FROM vGLTransaction_Detail WHERE DatePosted > @FirstDayOfLastMonth AND DatePosted < @LastDayOfLastMonth GROUP BY AcctCode
----This works alone and this is my desired output
SELECT AcctCode
,SUM(CASE WHEN DatePosted > @FirstDayOfLastMonth AND DatePosted < @LastDayOfLastMonth THEN Debit ELSE 0 END)
,SUM(Debit)[Debit]
,SUM(Credit)[Credit]
,SUM(Credit)[Ending]
FROM dbo.vGLTransaction_Detail
GROUP BY AcctCode
Для SQL Server 2012+ вы можете использовать IIF
:
SUM(IIF(atePosted > @FirstDayOfLastMonth AND DatePosted < @LastDayOfLastMonth, Debit, 0))
Я считаю это более понятным для таких случаев.