Im not PostgreDev, got problem with returning just one value in subquery.
select * from
(
select m_id from TableA where m_id = 236779
)Main
inner join
(
select m_m_id as l_m_id,date_created as l_date_created
from TableB
where
proc_type <> '-'
order by date_created desc limit 1
) CheckLastCode on (Main.m_id = CheckLastCode.l_m_id)
Will return empty set.
When I take down limit 1
select * from
(
select m_id from TableA where m_id = 236779
)Main
inner join
(
select m_m_id as l_m_id,date_created as l_date_created
from TableB
where
proc_type <> '-'
order by date_created desc
) CheckLastCode on (Main.m_id = CheckLastCode.l_m_id)
Will return all from TableB.
Im trying to have just last value from tableB
@EDIT It should work for every m_id in tableA
So my output: M_ID | MAX(DATE_CREATED) for that M_ID| ...
Here is the SQL Fiddle that demonstrates the following query:
If your tables have a lot of records you may want to apply a range in a WHERE clause to speed up the query, like so: