This is often approached as a filtering query rather than an aggregation query. You want to join the tables and then filter so only the most recent row is maintained.
There are multiple different ways to do this, but a correlated subquery often has good performance:
select T.kId, T.tKode, M.hId,M.tDate as newestTDate
from _PFaq T join
_mUser M
on T.kId = M.kId and T.tKode = M.tKode
where m.tDate = (select max(m2.tDate)
from _mUser m2
where m2.kId = M.kId and
m2.tKode = M.tKode
);
That said, the two columns you are selecting are in both tables. Unless you are using the T
table for filtering, you don't even need the JOIN
:
select M.kId, M.tKode, M.hId,M.tDate as newestTDate
from _mUser M
where m.tDate = (select max(m2.tDate)
from _mUser m2
where m2.kId = M.kId and
m2.tKode = M.tKode
);
The best index for this query is _mUser(kId, tKode, tDate desc)
.