SQLite Forum

Novice question concerning adding conditional incremental integer counter
Login
Thank you very much.  I came across this type of method in searching but didn't realize that row_number() was a window function and thought it was available only in sql server. I should've been smart enough to have just typed row_number in the SQLite search box.

The best I came up with was to count the number of 'punc2' rows in a subquery. It appears to work but I assume is not very efficient.

  select book_no, chapter_no, verse_no, strongs_no, index_no,
    (case when strongs_no = 'punc2' then null
          else
               index_no - (select count(*)
                           from bh_interlinear
                           where book_no=b.book_no
                           and chapter_no=b.chapter_no
                           and verse_no=b.verse_no
                           and strongs_no='punc2'
                           and index_no <= b.index_no)
     end ) as new_index
  from bh_interlinear b
  where b.book_no=1
    and b.chapter_no=1
    and b.verse_no < 4


┌─────────┬────────────┬──────────┬────────────┬──────────┬───────────┐
│ book_no │ chapter_no │ verse_no │ strongs_no │ index_no │ new_index │
├─────────┼────────────┼──────────┼────────────┼──────────┼───────────┤
│ 1       │ 1          │ 1        │ H7225      │ 1        │ 1         │
│ 1       │ 1          │ 1        │ H1254      │ 2        │ 2         │
│ 1       │ 1          │ 1        │ H430       │ 3        │ 3         │
│ 1       │ 1          │ 1        │ H853       │ 4        │ 4         │
│ 1       │ 1          │ 1        │ H8064      │ 5        │ 5         │
│ 1       │ 1          │ 1        │ H853       │ 6        │ 6         │
│ 1       │ 1          │ 1        │ H776       │ 7        │ 7         │
│ 1       │ 1          │ 1        │ punc2      │ 8        │           │
│ 1       │ 1          │ 2        │ H776       │ 1        │ 1         │
│ 1       │ 1          │ 2        │ H1961      │ 2        │ 2         │
│ 1       │ 1          │ 2        │ H8414      │ 3        │ 3         │
│ 1       │ 1          │ 2        │ punc2      │ 4        │           │
│ 1       │ 1          │ 2        │ H922       │ 5        │ 4         │
│ 1       │ 1          │ 2        │ punc2      │ 6        │           │
│ 1       │ 1          │ 2        │ H2822      │ 7        │ 5         │
│ 1       │ 1          │ 2        │ H5921      │ 8        │ 6         │
│ 1       │ 1          │ 2        │ H6440      │ 9        │ 7         │
│ 1       │ 1          │ 2        │ H8415      │ 10       │ 8         │
│ 1       │ 1          │ 2        │ punc2      │ 11       │           │
│ 1       │ 1          │ 2        │ H7307      │ 12       │ 9         │
│ 1       │ 1          │ 2        │ H430       │ 13       │ 10        │
│ 1       │ 1          │ 2        │ H7363      │ 14       │ 11        │
│ 1       │ 1          │ 2        │ H5921      │ 15       │ 12        │
│ 1       │ 1          │ 2        │ H6440      │ 16       │ 13        │
│ 1       │ 1          │ 2        │ H4325      │ 17       │ 14        │
│ 1       │ 1          │ 2        │ punc2      │ 18       │           │
│ 1       │ 1          │ 3        │ H559       │ 1        │ 1         │
│ 1       │ 1          │ 3        │ H430       │ 2        │ 2         │
│ 1       │ 1          │ 3        │ punc2      │ 3        │           │
│ 1       │ 1          │ 3        │ H1961      │ 4        │ 3         │
│ 1       │ 1          │ 3        │ H216       │ 5        │ 4         │
│ 1       │ 1          │ 3        │ punc2      │ 6        │           │
│ 1       │ 1          │ 3        │ H1961      │ 7        │ 5         │
│ 1       │ 1          │ 3        │ H216       │ 8        │ 6         │
│ 1       │ 1          │ 3        │ punc2      │ 9        │           │
└─────────┴────────────┴──────────┴────────────┴──────────┴───────────┘