Skip to content

Instantly share code, notes, and snippets.

@SeoRoman
Last active March 1, 2018 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeoRoman/1ce9213817e3ab78529b3d2155faaca0 to your computer and use it in GitHub Desktop.
Save SeoRoman/1ce9213817e3ab78529b3d2155faaca0 to your computer and use it in GitHub Desktop.
SELECT del.* FROM del AS a
JOIN accts ON a.acctno = accts.acctno AND accts.status IN ('1', '2')
JOIN trans ON a.acctno = trans.acctno AND trans.code NOT IN ('1', '2')
AND ( SELECT MAX(trans.dte) FROM trans AS b WHERE b.acctno = a.acctno ORDER BY b.dte DESC ) < {date}
WHERE a.days > 90
// # Problem
// I need to retrieve ALL Accounts that are at least 90+ in the Del.days column
// Pull acct info along with that... and only retrieve, IF the last transaction (most recent)
// is less than {x} date... for instance if I say 10/31/2017 ...
// So I need to All Accounts that are > 90 days where LAST transaction < 10/31/2017
// # Accts Table
acctno, fname, lname
1, Test, Test
2, Other, Other
3, Another, Another
// # Trans Table
acctno, dte, type
1, 09/02/2017, 'PM'
1, 10/20/2017, 'PM'
2, 10/20/2017, 'PM',
3, 10/10/2017, 'PM'
// Del Table
acctno, days
1, 150
2, 92
3, 40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment