Immediate disclaimer: Its been a few years since I seriously did SQL, returning recently.
Given a table:
logical_id, instance_id, node_id, state, time_stamp
1, 1, a, starting, 1300003234
2, 1, a, up, 1300003235
**3, 1, b, starting, 1300003237**
4, 1, a, completed, 1300003239
5, 1, a, shutdown, 1300003240
Given a specific instance_id (e.g. 1) I want to get the last state record (single row- highlighted in bold) of the authoritive node, the authoritive node is the most recent one to have entered a ‘starting’, in the absence of any node entering a starting stage it just the most recent node to be logged.
I figured I do some form group by node_id with order by (rather than filter) boolean(state = ‘starting), time_stamp desc, but I cant seem to get this right, in addition I think the group by would have to come after the ordering.
Im using postgres 9.x
This will retrieve the most recent stating node, if it exists, otherwise it will simply return the most recent non-starting node.