Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This returns Process 1 as required from your dataset:</p> <pre><code>with nextSteps as ( select s.ProcessID , disapprove = case when s.ApproverID &lt;= n.ApproverID or n.ApproverID is null then 0 else 1 end from steps s outer apply ( select top 1 ApproverID from steps n where s.ProcessID = n.ProcessID and s.StepStartDate &lt; n.StepStartDate order by n.StepStartDate ) n ) select ProcessID from nextSteps group by ProcessID having sum(disapprove) = 0 </code></pre> <p><a href="http://sqlfiddle.com/#!3/40a80/6" rel="nofollow">SQL Fiddle with demo</a>.</p> <p>Basically for each process step we get the next step in the same process, and check this next step is by the same or a greater approver.</p> <p>If one or more rows fails, don't return the ProcessID.</p> <p><strong>Edit after comment:</strong></p> <pre><code>with nextSteps as ( select s.ProcessID , disapprove = case when s.ApproverID = n.ApproverID then 1 else 0 end from steps s outer apply ( select top 1 ApproverID from steps n where s.ProcessID = n.ProcessID and s.StepStartDate &lt; n.StepStartDate order by n.StepStartDate ) n ) select ProcessID from nextSteps group by ProcessID having sum(disapprove) &gt; 0 </code></pre> <p><a href="http://sqlfiddle.com/#!3/3fdce1/2" rel="nofollow">SQL Fiddle with demo</a>.</p> <p>This new slightly changed query works for the following approver patterns, as shown in the SQL Fiddle:</p> <p><strong>ProcessID 1: A,B,B,C</strong> - Has consecutive approvers, should be returned.</p> <p><strong>ProcessID 2: A,B,C,B</strong> - Has no consecutive approvers, should not be returned.</p> <p><strong>ProcessID 3: A,B,C,D</strong> - Has no consecutive approvers, should not be returned.</p> <p>So very similar, checking the next row for each step in a ProcessID, then returning the ProcessID if there is at least one consecutive approver identified.</p> <p>I've also checked against the data in the SQL Fiddle you added - seems to be working:</p> <p><a href="http://sqlfiddle.com/#!3/48589/7" rel="nofollow">SQL Fiddle with more data</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload