I try to write an sql request to find the last record for each group, the is the layout of my table:
--------------------------------------
| id | Group | message | Status | date|
-------------------------------------
| 1 | A | msg1 | sent | dt |
-------------------------------------
| 2 | A | msg2 |deferred| dt2 |
-------------------------------------
| 3 | B | msg3 |deferred| dt |
-------------------------------------
| 4 | B | msg4 |deferred| dt |
-------------------------------------
| 5 | B | msg5 |Bounced | dt |
-------------------------------------
| 6 | C | msg6 |sent | dt |
-------------------------------------
| 7 | D | msg7 |deferred| dt |
-------------------------------------
| 8 | D | msg8 |deferred| dt |
-------------------------------------
| 9 | D | msg9 |sent | dt |
-------------------------------------
And i try to get this output:
--------------------------------------
| id | Group | message | Status | date|
-------------------------------------
| 2 | A | msg2 |deferred| dt |
-------------------------------------
| 5 | B | msg5 |bounced | dt |
-------------------------------------
| 8 | D | msg8 |deferred| dt |
-------------------------------------
that mean that i want, for each group, the last "deferred" or "bounced" message using the date that i have in my table. I start by doing this request, but i don't know how to modify it to get the result that i want:
SELECT ee.Group, ee.message
FROM email_errors ee
LEFT JOIN email_errors ee2 ON ( ee.Group = ee2.Group
AND ee.id < ee2.id )
WHERE ee2.id IS NULL
AND ee2.status <>0
I note the ee.status <> 0 because in my real table i replaced the 3 possible status bu 0,1,2, and i replaced the sent status by 0.