I am using SQL 2008R2 and need some assistance.
This query gives me the result I need except the CLIENT_CODE (in the C table) that corresponds with the LASTEST_TRANS:
SELECT
max(T.TRANS_ENTRY_DATE)AS LATEST_TRANS,
P.PAYEE_CODE,
P. PAYEE_NAME
FROM
{PREMIUMS_TRANSACTION] TRANS
INNER JOIN {PAYEES] P ON T.PAYEE_CODE = P.PAYEES_ID
INNER JOIN [CLIENTS] C ON T.BILLTO_ID = C.CLIENTS_ID
WHERE
NOT(T.TRANS_ENTRY_DATE) IS NULL AND
(P.DATE_OFF IS NULL)
GROUP BY
P. PAYEE_NAME,P.PAYEE_CODE
Desired results:
LATEST_TRANS PAYEE PAYEE_NAME CLIENT_CODE
5/14/2020 ABC ABC Ins CLIENTA
6/18/2018 AAC Safety CLIENTB
7/23/2019 AAP General CLIENTF
5/15/2020 ARC Arch CLIENTA
I've been searching and it seems I need a subquery, but I can't seem to translate their query to mine. Any help you could provide would be appreciated!
This query gives me the result I need except the CLIENT_CODE (in the C table) that corresponds with the LASTEST_TRANS:
SELECT
max(T.TRANS_ENTRY_DATE)AS LATEST_TRANS,
P.PAYEE_CODE,
P. PAYEE_NAME
FROM
{PREMIUMS_TRANSACTION] TRANS
INNER JOIN {PAYEES] P ON T.PAYEE_CODE = P.PAYEES_ID
INNER JOIN [CLIENTS] C ON T.BILLTO_ID = C.CLIENTS_ID
WHERE
NOT(T.TRANS_ENTRY_DATE) IS NULL AND
(P.DATE_OFF IS NULL)
GROUP BY
P. PAYEE_NAME,P.PAYEE_CODE
Desired results:
LATEST_TRANS PAYEE PAYEE_NAME CLIENT_CODE
5/14/2020 ABC ABC Ins CLIENTA
6/18/2018 AAC Safety CLIENTB
7/23/2019 AAP General CLIENTF
5/15/2020 ARC Arch CLIENTA
I've been searching and it seems I need a subquery, but I can't seem to translate their query to mine. Any help you could provide would be appreciated!