title: How to get next step
body: <p>I have the table request_step with dummy info like bellow</p>
  
  <pre><code>id,request_id,state,name,step_number
  *2,1,pending,step a,1
  1,1,pending,step b,2
  3,1,pending,step c,3
  4,1,pending,step d,4
  5,2,accepted,step a,1
  6,2,accepted,step b,2
  *7,2,pending,step c,3
  8,2,pending,step d,4
  9,7,accepted,step a,1
  10,7,accepted,step b,2
  11,7,accepted,step c,3
  14,7,accepted,step d,4
  *13,7,pending,step e,5
  12,7,pending,step f,6
  15,8,accepted,step a,1
  17,8,rejected,step b,2
  16,8,pending,step c,3   --&gt; not this one because the previous step is rejected
  18,9,accepted,step a,1
  19,9,accepted,step b,2
  </code></pre>
  
  <p>I want to take the next steps(pending state) per request_id and the previous steps must not have any rejected state. I mark an asterisk before line to show which one I want to query</p>
  
  <p>I already have a sql script to return me all the next steps but I can not find a solution to check if the previous steps have rejected</p>
  
  <pre><code>select rs1.id, rs1.step_number, rs0
  from (
  SELECT min(rs1.step_number) as min_sn, rs1.request_id as rid
      FROM request_step rs1
      where rs1.state = 'pending'
      GROUP BY rs1.request_id
  ) rs0
  left join request_step rs1 on rs1.step_number = rs0.min_sn and rs1.request_id = rs0.rid
  order by rs1.request_id
  </code></pre>