SQLite Forum

Novice question concerning adding conditional incremental integer counter
Login
Like this perhaps?

```
create table x
(
  book_no integer,
  chapter_no integer,
  verse_no integer,
  index_no integer,
  strongs_no text,
  desired_new_index integer
);

insert into x (book_no, chapter_no, verse_no, index_no, strongs_no) values
(1, 1, 2, 1, 'H776'),
(1, 1, 2, 2, 'H1961'),
(1, 1, 2, 3, 'H8414'),
(1, 1, 2, 4, 'punc2'),
(1, 1, 2, 5, 'H922'),
(1, 1, 2, 6, 'punc2'),
(1, 1, 2, 7, 'H2822'),
(1, 1, 2, 8, 'H5921'),
(1, 1, 2, 9, 'H6440'),
(1, 1, 2, 10, 'H8415'),
(1, 1, 2, 11, 'punc2'),
(1, 1, 2, 12, 'H7307'),
(1, 1, 2, 13, 'H430'),
(1, 1, 2, 14, 'H7363'),
(1, 1, 2, 15, 'H5921'),
(1, 1, 2, 16, 'H6440'),
(1, 1, 2, 17, 'H4325'),
(1, 1, 2, 18, 'punc2'),
(1, 1, 3, 1, 'H430');


update x as dst
   set desired_new_index = ni
  from (
        select _rowid_,
               row_number() over (partition by book_no, chapter_no, verse_no order by index_no) as ni
          from x
         where strongs_no != 'punc2'
       ) as src
 where src._rowid_ == dst._rowid_
;

select * from x;
┌─────────┬────────────┬──────────┬──────────┬────────────┬───────────────────┐
│ book_no │ chapter_no │ verse_no │ index_no │ strongs_no │ desired_new_index │
├─────────┼────────────┼──────────┼──────────┼────────────┼───────────────────┤
│ 1       │ 1          │ 2        │ 1        │ H776       │ 1                 │
│ 1       │ 1          │ 2        │ 2        │ H1961      │ 2                 │
│ 1       │ 1          │ 2        │ 3        │ H8414      │ 3                 │
│ 1       │ 1          │ 2        │ 4        │ punc2      │                   │
│ 1       │ 1          │ 2        │ 5        │ H922       │ 4                 │
│ 1       │ 1          │ 2        │ 6        │ punc2      │                   │
│ 1       │ 1          │ 2        │ 7        │ H2822      │ 5                 │
│ 1       │ 1          │ 2        │ 8        │ H5921      │ 6                 │
│ 1       │ 1          │ 2        │ 9        │ H6440      │ 7                 │
│ 1       │ 1          │ 2        │ 10       │ H8415      │ 8                 │
│ 1       │ 1          │ 2        │ 11       │ punc2      │                   │
│ 1       │ 1          │ 2        │ 12       │ H7307      │ 9                 │
│ 1       │ 1          │ 2        │ 13       │ H430       │ 10                │
│ 1       │ 1          │ 2        │ 14       │ H7363      │ 11                │
│ 1       │ 1          │ 2        │ 15       │ H5921      │ 12                │
│ 1       │ 1          │ 2        │ 16       │ H6440      │ 13                │
│ 1       │ 1          │ 2        │ 17       │ H4325      │ 14                │
│ 1       │ 1          │ 2        │ 18       │ punc2      │                   │
│ 1       │ 1          │ 3        │ 1        │ H430       │ 1                 │
└─────────┴────────────┴──────────┴──────────┴────────────┴───────────────────┘
```