title: XQuery tumbling window: group by start item of first window
body: <p>Using BaseX 9.7.3, I have a sorted list of names that has been produced using a <code>tumbling window</code> clause.</p>
  <p>A snippet of the data looks like this:</p>
  <pre><code>&lt;data&gt;
    &lt;group&gt;
      &lt;key id=&quot;0c7b0bca-0349-489c-b45f-2612f3134a76&quot;&gt;ovid&lt;/key&gt;
      &lt;key id=&quot;f77ab9c2-0be3-4348-809d-ab245e630f81&quot;&gt;ovid 43 b c-17 or 18 a d&lt;/key&gt;
    &lt;/group&gt;
    &lt;group&gt;
      &lt;key id=&quot;39b9d6c2-85a5-4c72-a83e-2a52e548fc3b&quot;&gt;ovid 43 bc&lt;/key&gt;
      &lt;key id=&quot;acf5b3c0-8fd4-4e0c-950b-a40683bab431&quot;&gt;ovid 43 bc-17 ad&lt;/key&gt;
      &lt;key id=&quot;cc57be53-9ca8-4b5e-97cf-1aeca798cded&quot;&gt;ovid 43 bc-17 ad or 18 a&lt;/key&gt;
      &lt;key id=&quot;8395e750-1e52-4152-9d37-8c8f4e389fd3&quot;&gt;ovid 43 bc-17 ad or 18 ad&lt;/key&gt;
    &lt;/group&gt;
    &lt;group&gt;
      &lt;key id=&quot;0be07fc6-d9bf-4d56-8352-1885b4dd6574&quot;&gt;ovid 43 bc-17 or 18&lt;/key&gt;
      &lt;key id=&quot;e3aafc69-56b0-4632-a96c-26ca448c6c2d&quot;&gt;ovid 43 bc-17 or 18 ad&lt;/key&gt;
    &lt;/group&gt;
    &lt;group&gt;
      &lt;key id=&quot;f9615365-4a32-442b-9e20-9c5abb0e6fa0&quot;&gt;ovide&lt;/key&gt;
      &lt;key id=&quot;c7b45a8d-79a3-4e79-b32b-8d918f67a7b0&quot;&gt;ovide 0043 av j-c-0017&lt;/key&gt;
    &lt;/group&gt;
  &lt;/data&gt;
  </code></pre>
  <p>I would like to further group the data so that, in this example, a group would begin with &quot;ovid&quot; and end with &quot;ovid 43 bc-17 or 18 ad.&quot;</p>
  <p>Desired output:</p>
  <pre><code>&lt;data&gt;
    &lt;group&gt;
      &lt;key id=&quot;0c7b0bca-0349-489c-b45f-2612f3134a76&quot;&gt;ovid&lt;/key&gt;
      &lt;key id=&quot;f77ab9c2-0be3-4348-809d-ab245e630f81&quot;&gt;ovid 43 b c-17 or 18 a d&lt;/key&gt;  
      &lt;key id=&quot;39b9d6c2-85a5-4c72-a83e-2a52e548fc3b&quot;&gt;ovid 43 bc&lt;/key&gt;
      &lt;key id=&quot;acf5b3c0-8fd4-4e0c-950b-a40683bab431&quot;&gt;ovid 43 bc-17 ad&lt;/key&gt;
      &lt;key id=&quot;cc57be53-9ca8-4b5e-97cf-1aeca798cded&quot;&gt;ovid 43 bc-17 ad or 18 a&lt;/key&gt;
      &lt;key id=&quot;8395e750-1e52-4152-9d37-8c8f4e389fd3&quot;&gt;ovid 43 bc-17 ad or 18 ad&lt;/key&gt;  
      &lt;key id=&quot;0be07fc6-d9bf-4d56-8352-1885b4dd6574&quot;&gt;ovid 43 bc-17 or 18&lt;/key&gt;
      &lt;key id=&quot;e3aafc69-56b0-4632-a96c-26ca448c6c2d&quot;&gt;ovid 43 bc-17 or 18 ad&lt;/key&gt;
    &lt;/group&gt;
    &lt;group&gt;
      &lt;key id=&quot;f9615365-4a32-442b-9e20-9c5abb0e6fa0&quot;&gt;ovide&lt;/key&gt;
      &lt;key id=&quot;c7b45a8d-79a3-4e79-b32b-8d918f67a7b0&quot;&gt;ovide 0043 av j-c-0017&lt;/key&gt;
    &lt;/group&gt;
  &lt;/data&gt;
  </code></pre>
  <p>I have the following query, but it simply reproduces the input document:</p>
  <pre><code>&lt;data&gt;{
    for tumbling window $entry in /*/group/key  
    start $s at $sp previous $sprev next $snext when starts-with($snext, $s)
    end $e at $ep next $enext when not(starts-with($enext, $e)) 
    return  
      &lt;group&gt;{
        for $k in $entry
        return (
          &lt;key id=&quot;{$k/@id}&quot;&gt;{data($k)}&lt;/key&gt;
        )      
      }&lt;/group&gt;         
  }&lt;/data&gt;
  </code></pre>
  <p>Is it possible to compare the start item of the first group (&quot;ovid&quot;) to subsequent entries that start with that token? I want to exclude &quot;ovide,&quot; even though it starts with &quot;ovid.&quot;</p>