I have a table A as foolows:
id name nextid orderset
19 done 4
21 init 27 1
24 sent-req 19 3
27 offer-req 24 2
the orderset column gives me the order of the status: init -> offer-req -> sent-req -> done
I need to write a query that for a given id, it gives the id of the next status.
That should be easy:
select id
from A
where orderset= (select orderset+1 from A where id=Any_Given_ID)
That works in all cases except the last id... as there is no next to id=19, In the case of the last one I want the query to return 0.
I thought of doing a check with Max(orderset) and select order+1 from A where id=Any_Given_ID but I just can't seem to make it work.
Is it doable in a query or do I have to write a function for this operation?