Try this:
;WITH cte1
AS (
SELECT PAYID
,USERID
,PACKID
,RANK() OVER (
PARTITION BY USERID ORDER BY PAYID DESC
) AS Rnk
FROM table3
)
,cte2
AS (
SELECT PAYID
,USERID
,PACKID
FROM cte1
WHERE Rnk = 1
)
SELECT t3.PAYID
,t3.USERID
,t3.PACKID
FROM table3 t3
INNER JOIN cte2 ON t3.PAYID <= cte2.PAYID
AND t3.USERID = cte2.USERID
AND t3.PACKID = cte2.PACKID
I made some assumptions:
1. PAYID will increase as more and more records get added. In other words, the greater the PAYID, the more recent it is. Your usage of MAX(PAYID) seems to suggest my assumption is correct.
2. You need all repetitions of a USERID, PACKID combo if those repetitions match the USERID, PACKID combo corresponding to the latest PAYID for that USER. So, there might be more than 2 repetitions. You will get all repetitions in my query.