In reference to the post "Retrieving the last record in each group", I adapted my query (using postgresql) to look for the last object of MATABLE on the zone MAZONE whose attribute created_at is the most recent:
SELECT
p1.id,
p2.id
FROM MATABLE p1
LEFT JOIN MATABLE p2
ON (p1.zone_id = p2.zone_id AND p1.created_at < p2.created_at)
WHERE
p2.id IS NULL
AND p1.zone_id = 'MAZONE';
The problem is that I get the following response:
id | id
----+----
(0 rows)
However, if I filter my table like this:
SELECT
p1.id,
p2.id
FROM MATABLE p1
LEFT JOIN (SELECT * FROM MATABLE WHERE zone_id='MAZONE') p2
ON (p1.zone_id = p2.zone_id AND p1.created_at < p2.created_at)
WHERE
p2.id IS NULL
AND p1.zone_id = 'MAZONE';
I get a result:
id | id
------+----
12716 |
(1 row)
Any idea about this problem?
Thanks in advance!