title: SQLite finding query-specific index of sub-query
body: <p>So I know just enough SQL to be dangerous and not much else. I'm developing an application that needs to display sections of data from a SQLite database, and be able to iterate through some of the data based on certain criteria.</p>
  
  <p>So I have a table that looks like this:</p>
  
  <pre><code>create table packets( id INTEGER PRIMARY KEY, timestamp varchar(32), type varchar(32), source varchar(32), destination varchar(32), channel varchar(16), first_sequence integer, last_sequence integer, missing integer );
  </code></pre>
  
  <p>Then I have a view that will display some or all of the data based on certain criteria.</p>
  
  <p>We're talking about a table with millions of rows, so I can't load it all into memory. So I have a view class that asks for each item individually based upon the index in the view.</p>
  
  <p>So for instance, if I am displaying all of the packets that have a channel value of 'C', and the view is ready to draw the first item I would issue this query to the database:</p>
  
  <pre><code>SELECT * FROM packets WHERE channel='C' LIMIT 1 OFFSET 0
  </code></pre>
  
  <p>When it's ready to draw the second item I do</p>
  
  <pre><code>SELECT * FROM packets WHERE channel='C' LIMIT 1 OFFSET 1
  </code></pre>
  
  <p>etc...</p>
  
  <p>This works fine. I realize it's probably not the most optimal way to do it, but I'm just trying to learn how to do it the simplest way with SQLite first and then worry about optimizing later.</p>
  
  <p>Now the problem is I need to be able to find items within this result set based on another search criteria, and get the index within the original result set.</p>
  
  <p>For instance I need to be able to iterate through the items within the original result set that has a "missing" value of 1, and figure out the view index of those items.</p>
  
  <p>If I'm using the entire data set in the table I think I can do it with something similar to this:</p>
  
  <pre><code>SELECT rowid FROM packets WHERE rowid &gt; [currentlySelectedRowID] AND missing=1 LIMIT 1
  </code></pre>
  
  <p>But when I need an index within a subset of data, the rowid doesn't really help me, and I have no idea how to find the index within the subset without iterating through all of the items individually myself.</p>
  
  <p>Any ideas on how to go about doing this with a SQL query, or pointers to relevant documentation or tutorials would be greatly appreciated.</p>
  
  <p>Thanks!</p>
  
  <p>(note: the question may not make sense as it's a little convoluted in my mind of how to explain it, so if something is not clear I will try to elaborate.)</p>
  
  <p>Edit: actually I found that doing this in C code is sufficient from a performance perspective by querying the entire result set after the initial index by doing a LIMIT -1 OFFSET [startIndex], and then stepping through until I find the next missing item. It would still be nice to know if there is a way to do it with just an SQL statement, though, for future reference.</p>