Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I might approach it this way:</p> <ol> <li><p>Create a stored procedure to contain the sequence testing</p> <p>a. Select the rows by order ID into a local table @variable in the order of history ID ascending with an IDENTITY column with seed 1 and increment 1</p> <p>b. What you do next depends on how complicated/fancy/maintainable you want this to be. A quick and dirty solution would be to build a comma-separated list of the IDs from the table variable, in order, and compare them against some hard-coded sequence strings.</p> <p>There are <a href="https://stackoverflow.com/questions/194852/concatenate-many-rows-into-a-single-text-string">several techniques for building comma-delimited strings</a> from multiple rows.</p> <p>c. For a fancier solution, you could create a new table (something like "HistoryTestSequence") that looks something like this (to illustrate your 1,4,3,2 test):</p> <pre><code>TestNumber | SequenceOrder | CompareValue -----------+---------------+------------- 1 | 1 | 1 1 | 2 | 4 1 | 3 | 3 1 | 4 | 2 </code></pre> <p>You could then try to <code>JOIN</code> to this from the table variable, matching the identity column to the <code>SequenceOrder</code> column and the history ID to the <code>CompareValue</code> column, <code>WHERE</code> the <code>TestNumber = 1</code>. If the <code>COUNT()</code> of the rows returned after the JOIN equals the <code>COUNT(*) FROM HistoryTestSequence WHERE TestNumber = 1</code>, then you have matched your sequence. You just have to repeat the test for each set of possible sequences, which you could do in a loop, or manually.</p></li> </ol> <p>If I had some more time I'd love to put together an example, but I'll let you give it a shot for now. The latter example is probably overkill. </p> <p>Lastly, just keep in mind that testing against identity columns is dangerous -- IDs created by sequences on one server might have different ones on another server (such as if you had production and UAT SQL server instances that are a little out of sync). </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. This table or related slice is empty.
    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