Well I just can't seem to figure this out. I'm on day two of messing around with this and it's time to enlist some help.
Here's the SQL that works:
SELECT rt1.request_id, rt1.status
FROM request_transactions rt1
INNER JOIN (
SELECT max(id) AS max_id
FROM request_transactions
GROUP BY request_id
) AS rt2 ON rt1.id = rt2.max_id;
How in the world do I write this using the Cakephp 3 query builder? I'm completely stuck in regards to the inner join with an aggregate function (max).
Many thanks in advance.
EDIT: Here's the table columns and data, if that helps:
SELECT id, user_id, created, modified FROM requests;
id | user_id | created | modified
----+---------+---------------------+---------------------
94 | 101 | 2017-07-04 18:20:47 | 2017-07-04 18:21:26
95 | 101 | 2017-07-04 18:48:14 | 2017-07-04 18:48:14
96 | 101 | 2017-07-04 18:48:40 | 2017-07-04 18:48:40
(3 rows)
SELECT id, status, created, request_id FROM request_transactions;
id | status | created | request_id
-----+----------+---------------------+------------
127 | created | 2017-07-04 18:20:47 | 94
128 | read | 2017-07-04 18:21:03 | 94
129 | rejected | 2017-07-04 18:21:26 | 94
130 | created | 2017-07-04 18:48:14 | 95
131 | created | 2017-07-04 18:48:40 | 96
(5 rows)
SELECT rt1.request_id, rt1.status
FROM request_transactions rt1
INNER JOIN (
SELECT max(id) AS max_id
FROM request_transactions
GROUP BY request_id
) AS rt2 ON rt1.id = rt2.max_id;
request_id | status
------------+----------
96 | created
94 | rejected
95 | created
(3 rows)