I have two files: apt, and appointment_history. The second file has multiple records referencing single records in apt.
How to I get only the last record in appointment history that is referencing the record in apt from within the subquery?
Edit#1: The question is not so much how to group, but rather how to pass an outside value (appointment_recid) into the subquery without grouping the entire appointment_history file on non-used appointment_recid's. So I don't think this is a duplicate question. (Although being a noobie, it might turn out the same).
PostgreSQL 9.3
ERROR: invalid reference to FROM-clause entry for table "apt" SQL state: 42P01 Hint: There is an entry for table "apt", but it cannot be referenced from this part of the query.
select apt.*, h.*
from apt
join appointment_history h on (h.appointment_recid = apt.appointment_recid)
join (
select max(tposted) as tposted
from appointment_history a
where a.appointment_recid = apt.appointment_recid) currenthx
on (currenthx.tposted = h.tposted)
TIA