if (@I_tInactiveAccounts = 0)
begin
EXEC (
'select d.*, isnull([s].[YEAR1], 0) as [YEAR1], isnull([s].[JRNENTRY], 0) as [JRNENTRY],
isnull([s].[RCTRXSEQ], 0.00000) as [RCTRXSEQ], isnull([s].[SOURCDOC], '''') as [SOURCDOC],
isnull([s].[REFRENCE], '''') as [REFRENCE], isnull([s].[DSCRIPTN], '''') as [DSCRIPTN],
isnull([s].[TRXDATE], ''1/1/1900'') as [TRXDATE], isnull([s].[TRXSORCE], '''') as [TRXSORCE],
isnull([s].[ACTINDX], 0) as [ACTINDX], isnull([s].[POLLDTRX], 0) as [POLLDTRX], isnull([s].[LASTUSER], '''') as [LASTUSER],
isnull([s].[LSTDTEDT], ''1/1/1900'') as [LSTDTEDT], isnull([s].[ORGNTSRC], '''') as [ORGNTSRC],
isnull([s].[ORGNATYP], 0) as [ORGNATYP], isnull([s].[QKOFSET], 0) as [QKOFSET], isnull([s].[SERIES], 0) as [SERIES],
isnull([s].[ORTRXTYP], 0) as [ORTRXTYP], isnull([s].[ORCTRNUM], '''') as [ORCTRNUM], isnull([s].[ORMSTRID], '''') as [ORMSTRID],
isnull([s].[ORMSTRNM], '''') as [ORMSTRNM], isnull([s].[ORDOCNUM], '''') as [ORDOCNUM], isnull([s].[ORPSTDDT], ''1/1/1900'') as [ORPSTDDT],
isnull([s].[ORTRXSRC], '''') as [ORTRXSRC], isnull([s].[OrigDTASeries], 0) as [OrigDTASeries], isnull(s.[OrigSeqNum], 0) as [OrigSeqNum],
isnull([s].[SEQNUMBR], 0) as [SEQNUMBR], isnull([s].[DTA_GL_Status], 0) as [DTA_GL_Status], isnull(s.[DTA_Index], 0.00000) as [DTA_Index],
isnull([s].[NOTEINDX], 0.00000) as [NOTEINDX], isnull([s].[ICTRX], 0) as [ICTRX], isnull([s].[ORCOMID], '''') as [ORCOMID],
isnull([s].[ORIGINJE], 0) as [ORIGINJE], isnull([s].[PERIODID], 0) as [PERIODID], isnull([s].[CRDTAMNTD], 0.00000) as [CRDTAMNTD],
isnull([s].[DEBITAMTD], 0.00000) as [DEBITAMTD], isnull([s].[DOCDATE], ''1/1/1900'') as [DOCDATE], isnull([s].[PSTGNMBR], 0) as [PSTGNMBR],
isnull([s].[PPSGNMBR], 0) as [PPSGNMBR], isnull([s].[CorrespondingUnit], '''') as [CorrespondingUnit], isnull([s].[PERINDX], 0) as [PeriodIndex],
isnull([s].[PERNAME], '''') as [PeriodName], isnull([s].[DEX_ROW_ID], 0) as [DEX_ROW_IDD], case when [ACCTTYPE] = 1 then isnull([s].[DEBITAMTD], 0.00000) else 0 end as [DEBITAMT],
case when [ACCTTYPE] = 1 then isnull([s].[CRDTAMNTD], 0.00000) else 0 end as [CREDITAMT], [m].*, case when TRXDATE is null then 1 else DATEPART(month, TRXDATE) end as MonthIndex,
case when TRXDATE is null then ''Janurary'' else DateName(month, TRXDATE) end as MonthField, case when SOURCDOC is null then ''*No Transactions for this account*'' else '''' end as ''ActZeroDesc'',
case when ACTIVE = 0 then ''*Inactive account*'' else '''' end as ''ActInactiveDesc'', case when [s].[DEX_ROW_ID] is null then [BGNGBAL] else case when [s].[DEX_ROW_ID] = (select min([tmp].[DEX_ROW_ID])
from #GLTBDTemp tmp where [s].[ACTINDX] = [tmp].[ACTINDX] and [tmp].[TRXDATE] = (select min([tmp].[TRXDATE]) from #GLTBDTemp tmp where [s].[ACTINDX] = [tmp].[ACTINDX])) then [BGNGBAL] else 0 end end as Beg_Bal,
case when [s].[DEX_ROW_ID] is null then [ENDNGBAL] else case when [s].[DEX_ROW_ID] = (select min([tmp].[DEX_ROW_ID]) from #GLTBDTemp tmp where [s].[ACTINDX] = [tmp].[ACTINDX] ) then [ENDNGBAL] else 0 end end as End_Bal from '
+ @temptable + ' d left join #GLTBDTemp s on d.ACTINDX = s.ACTINDX left join GL00100 m on d.ACTINDX = m.ACTINDX where s.TRXDATE between '''
+ @I_dStartingDate + ''' and ''' + @I_dEndingDate + '''')
end
else
begin
EXEC (
'select d.*, isnull([s].[YEAR1], 0) as [YEAR1], isnull([s].[JRNENTRY], 0) as [JRNENTRY],
isnull([s].[RCTRXSEQ], 0.00000) as [RCTRXSEQ], isnull([s].[SOURCDOC], '''') as [SOURCDOC],
isnull([s].[REFRENCE], '''') as [REFRENCE], isnull([s].[DSCRIPTN], '''') as [DSCRIPTN],
isnull([s].[TRXDATE], ''1/1/1900'') as [TRXDATE], isnull([s].[TRXSORCE], '''') as [TRXSORCE],
isnull([s].[ACTINDX], 0) as [ACTINDX], isnull([s].[POLLDTRX], 0) as [POLLDTRX], isnull([s].[LASTUSER], '''') as [LASTUSER],
isnull([s].[LSTDTEDT], ''1/1/1900'') as [LSTDTEDT], isnull([s].[ORGNTSRC], '''') as [ORGNTSRC],
isnull([s].[ORGNATYP], 0) as [ORGNATYP], isnull([s].[QKOFSET], 0) as [QKOFSET], isnull([s].[SERIES], 0) as [SERIES],
isnull([s].[ORTRXTYP], 0) as [ORTRXTYP], isnull([s].[ORCTRNUM], '''') as [ORCTRNUM], isnull([s].[ORMSTRID], '''') as [ORMSTRID],
isnull([s].[ORMSTRNM], '''') as [ORMSTRNM], isnull([s].[ORDOCNUM], '''') as [ORDOCNUM], isnull([s].[ORPSTDDT], ''1/1/1900'') as [ORPSTDDT],
isnull([s].[ORTRXSRC], '''') as [ORTRXSRC], isnull([s].[OrigDTASeries], 0) as [OrigDTASeries], isnull(s.[OrigSeqNum], 0) as [OrigSeqNum],
isnull([s].[SEQNUMBR], 0) as [SEQNUMBR], isnull([s].[DTA_GL_Status], 0) as [DTA_GL_Status], isnull(s.[DTA_Index], 0.00000) as [DTA_Index],
isnull([s].[NOTEINDX], 0.00000) as [NOTEINDX], isnull([s].[ICTRX], 0) as [ICTRX], isnull([s].[ORCOMID], '''') as [ORCOMID],
isnull([s].[ORIGINJE], 0) as [ORIGINJE], isnull([s].[PERIODID], 0) as [PERIODID], isnull([s].[CRDTAMNTD], 0.00000) as [CRDTAMNTD],
isnull([s].[DEBITAMTD], 0.00000) as [DEBITAMTD], isnull([s].[DOCDATE], ''1/1/1900'') as [DOCDATE], isnull([s].[PSTGNMBR], 0) as [PSTGNMBR],
isnull([s].[PPSGNMBR], 0) as [PPSGNMBR], isnull([s].[CorrespondingUnit], '''') as [CorrespondingUnit], isnull([s].[PERINDX], 0) as [PeriodIndex],
isnull([s].[PERNAME], '''') as [PeriodName], isnull([s].[DEX_ROW_ID], 0) as [DEX_ROW_IDD], case when [ACCTTYPE] = 1 then isnull([s].[DEBITAMTD], 0.00000) else 0 end as [DEBITAMT],
case when [ACCTTYPE] = 1 then isnull([s].[CRDTAMNTD], 0.00000) else 0 end as [CREDITAMT], [m].*, case when TRXDATE is null then 1 else DATEPART(month, TRXDATE) end as MonthIndex,
case when TRXDATE is null then ''Janurary'' else DateName(month, TRXDATE) end as MonthField, case when SOURCDOC is null then ''*No Transactions for this account*'' else '''' end as ''ActZeroDesc'',
case when ACTIVE = 0 then ''*Inactive account*'' else '''' end as ''ActInactiveDesc'', case when [s].[DEX_ROW_ID] is null then [BGNGBAL] else case when [s].[DEX_ROW_ID] = (select min([tmp].[DEX_ROW_ID])
from #GLTBDTemp tmp where [s].[ACTINDX] = [tmp].[ACTINDX] and [tmp].[TRXDATE] = (select min([tmp].[TRXDATE]) from #GLTBDTemp tmp where [s].[ACTINDX] = [tmp].[ACTINDX])) then [BGNGBAL] else 0 end end as Beg_Bal,
case when [s].[DEX_ROW_ID] is null then [ENDNGBAL] else case when [s].[DEX_ROW_ID] = (select min([tmp].[DEX_ROW_ID]) from #GLTBDTemp tmp where [s].[ACTINDX] = [tmp].[ACTINDX] ) then [ENDNGBAL] else 0 end end as End_Bal from '
+ @temptable + ' d left join #GLTBDTemp s on d.ACTINDX = s.ACTINDX left join GL00100 m on d.ACTINDX = m.ACTINDX')
end