I am new to SQL Server. I am trying to increment through my journal records and keep a 'Running Balance' while incrementing through each record. I also need a CASE statement to re-add a transaction if it had a 'NSF' fee. My code is below. So far I have attempted the CASE statement, but it isn't working.... Any help is appreciated. Thanks!
set nocount on
IF OBJECT_ID('tempdb..#TEMP37') is not null
BEGIN
drop table #TEMP37
END
Declare @C_NSF_FeeUseFlagID AS INT
SET @C_NSF_FeeUseFlagID = 94
Declare @C_RunningBal AS INT
SET @C_RunningBal = 0
Declare @C_NSF_Fee AS Money
SET @C_NSF_Fee = 20.00
SELECT DISTINCT
cv.contactIdnt AS "ID"
, cv.credNumber AS Credential
, cv.contactName AS "Account"
, th.transactionType AS "Type"
, cast (th.reference as varchar(50) ) AS "Reference"
, j.remark AS "Control Number"
, ( - j.amount )AS "_$_Amount"
, j.transactiondate AS "_d_Trans. Date"
, j.entrydate AS "_d_Entry Date"
, j.depositdate AS "_d_Deposit Date"
, cast( th.BatchNumber as varchar(12) ) As "Batch Number"
, cv.divisionIdnt AS "_h_divisionIdnt"
, @C_RunningBal = CASE
WHEN (select j.description from Journal
WHERE Journal.Description = 'Bad Check Fee'
Then select sum(-j.amount) + @C_RunningBal AS "Running Balance"
--WHERE Journal.Description = 'NSF (Bad Check)'
--Then (sum(-j.amount) + @C_RunningBal) AS "Running Balance"
--WHERE Journal.Description = 'NSF Check'
--Then (sum(-j.amount) + @C_RunningBal) AS "Running Balance"
END
INTO #TEMP37
FROM Journal j
LEFT JOIN TransactionHeader th
ON j.TransactionHeaderIdnt = th.TransactionHeaderIdnt
LEFT JOIN CredentialView cv
ON cv.credentialIdnt= j.credentialIdnt
WHERE j.transactiondate
BETWEEN '20060101'
AND '20061201'
AND j.recordType = 'CR Summary'
UNION ALL
Select
cv.contactIdnt AS "ID"
, cv.credNumber AS Credential
, cv.contactName AS "Account"
, 'NSF' AS "Type"
, 'N/A' AS "Reference"
, j.remark AS "Control Number"
, ( - j.amount ) AS "_$_Amount"
, j.transactiondate AS "_d_Trans. Date"
, j.entrydate AS "_d_Entry Date"
, j.depositdate AS "_d_Deposit Date"
,'N/A' As "Batch Number"
, cv.divisionIdnt AS "_h_divisionIdnt"
,( -j.amount + @C_RunningBal) AS "Running Balance"
From Journal J
INNER JOIN CredentialView cv (NOLOCK)
On cv.CredentialIdnt = J.CredentialIdnt
INNER JOIN CredentialDefinition cd (NOLOCK)
ON cv.credentialDefinitionIdnt = cd.credentialDefinitionIdnt
INNER JOIN FeeDefinition fd (NOLOCK)
ON j.FeeDefinitionIdnt = fd.FeeDefinitionIdnt
AND fd.Active = '6'
INNER JOIN FeeUseFlagType fuft (NOLOCK)
ON fd.feeUseFlagID = fuft.feeUseFlagID
AND fuft.feeUseFlagID = @C_NSF_FeeUseFlagID
WHERE j.transactiondate
BETWEEN '20060101'
AND '20061201'
AND j.recordType = 'Fee'
ORDER BY th.transactionType, cv.credNumber
SELECT * FROM #TEMP37
set nocount on
IF OBJECT_ID('tempdb..#TEMP37') is not null
BEGIN
drop table #TEMP37
END
Declare @C_NSF_FeeUseFlagID AS INT
SET @C_NSF_FeeUseFlagID = 94
Declare @C_RunningBal AS INT
SET @C_RunningBal = 0
Declare @C_NSF_Fee AS Money
SET @C_NSF_Fee = 20.00
SELECT DISTINCT
cv.contactIdnt AS "ID"
, cv.credNumber AS Credential
, cv.contactName AS "Account"
, th.transactionType AS "Type"
, cast (th.reference as varchar(50) ) AS "Reference"
, j.remark AS "Control Number"
, ( - j.amount )AS "_$_Amount"
, j.transactiondate AS "_d_Trans. Date"
, j.entrydate AS "_d_Entry Date"
, j.depositdate AS "_d_Deposit Date"
, cast( th.BatchNumber as varchar(12) ) As "Batch Number"
, cv.divisionIdnt AS "_h_divisionIdnt"
, @C_RunningBal = CASE
WHEN (select j.description from Journal
WHERE Journal.Description = 'Bad Check Fee'
Then select sum(-j.amount) + @C_RunningBal AS "Running Balance"
--WHERE Journal.Description = 'NSF (Bad Check)'
--Then (sum(-j.amount) + @C_RunningBal) AS "Running Balance"
--WHERE Journal.Description = 'NSF Check'
--Then (sum(-j.amount) + @C_RunningBal) AS "Running Balance"
END
INTO #TEMP37
FROM Journal j
LEFT JOIN TransactionHeader th
ON j.TransactionHeaderIdnt = th.TransactionHeaderIdnt
LEFT JOIN CredentialView cv
ON cv.credentialIdnt= j.credentialIdnt
WHERE j.transactiondate
BETWEEN '20060101'
AND '20061201'
AND j.recordType = 'CR Summary'
UNION ALL
Select
cv.contactIdnt AS "ID"
, cv.credNumber AS Credential
, cv.contactName AS "Account"
, 'NSF' AS "Type"
, 'N/A' AS "Reference"
, j.remark AS "Control Number"
, ( - j.amount ) AS "_$_Amount"
, j.transactiondate AS "_d_Trans. Date"
, j.entrydate AS "_d_Entry Date"
, j.depositdate AS "_d_Deposit Date"
,'N/A' As "Batch Number"
, cv.divisionIdnt AS "_h_divisionIdnt"
,( -j.amount + @C_RunningBal) AS "Running Balance"
From Journal J
INNER JOIN CredentialView cv (NOLOCK)
On cv.CredentialIdnt = J.CredentialIdnt
INNER JOIN CredentialDefinition cd (NOLOCK)
ON cv.credentialDefinitionIdnt = cd.credentialDefinitionIdnt
INNER JOIN FeeDefinition fd (NOLOCK)
ON j.FeeDefinitionIdnt = fd.FeeDefinitionIdnt
AND fd.Active = '6'
INNER JOIN FeeUseFlagType fuft (NOLOCK)
ON fd.feeUseFlagID = fuft.feeUseFlagID
AND fuft.feeUseFlagID = @C_NSF_FeeUseFlagID
WHERE j.transactiondate
BETWEEN '20060101'
AND '20061201'
AND j.recordType = 'Fee'
ORDER BY th.transactionType, cv.credNumber
SELECT * FROM #TEMP37