Hello Mahmoud,
Thank you so much for this script!! I have a customer that has over 13,000 accounts and we found about 8000 that had never been used!!
I added additional columns to help with filtering and updating (Created Date, Account Index (to help with linking to other tables), Account Type, and Active (Yes/No))
We did find that some accounts had been used...but Netted to Zero on the GL20000 and GL30000 table. We filtered the Date Last Used and the Never Used columns in SmartList (I used this in SmartList Builder). Do you have any ideas on how to exclude Accounts that have history...even though Debits = Credits for a year/period/etc?
SELECT B.ACTNUMST 'Account Number' ,
A.ACTDESCR 'Account Description' ,
A.CREATDDT 'Created Date',
A.ACTINDX 'Account Index',
A.ACCTTYPE 'Account Type',
A.ACTIVE 'Active',
CASE WHEN ISNULL(D.TRX_Date, 0) < A.CREATDDT THEN 'Yes'
ELSE ''
END AS NeverUsed ,
ISNULL(D.TRX_Date, 0) AS 'Last Used' ,
DATEDIFF(YY, ISNULL(D.TRX_Date, 0), GETDATE()) 'Not Used Since (In Years)' ,
DATEDIFF(MM, ISNULL(D.TRX_Date, 0), GETDATE()) 'Not Used Since (In Months)' ,
DATEDIFF(DD, ISNULL(D.TRX_Date, 0), GETDATE()) 'Not Used Since (In Days)'
FROM dbo.GL00100 AS A
LEFT OUTER JOIN dbo.GL00105 AS B ON A.ACTINDX = B.ACTINDX
LEFT OUTER JOIN ( SELECT ACTINDX ,
MAX(TRX_Date) TRX_Date
FROM ( SELECT ACTINDX ,
MAX(TRXDATE) AS TRX_Date
FROM dbo.GL20000
GROUP BY ACTINDX
UNION ALL
SELECT ACTINDX ,
MAX(TRXDATE) AS TRX_Date
FROM dbo.GL30000
GROUP BY ACTINDX
) AS C
GROUP BY C.ACTINDX
) AS D ON B.ACTINDX = D.ACTINDX