I have the following table with data as shown below; linenr
s are monotonically increasing but not necessarily consecutive; when the key
field contains an ellipsis ...
that indicates an entry that is continued from above:
create table source (
linenr integer unique not null,
key text not null,
value text );
insert into source values
( 2, 'tags', 'a' ),
( 3, '...', 'b' ),
( 4, 'title', 'The Title' ),
( 5, 'note', 'this is' ),
( 6, '...', 'an EXAMPLE' ),
( 8, 'title', 'over' ),
( 9, '...', 'three' ),
( 10, '...', 'lines' ),
( 11, 'about', 'grouping' );
Now I want to have a view where a group number is assigned based on the content of the key
field; group numbers do not have to be consecutive but should be distinct for each group of rows that starts with a key other than ...
and continues thru all lines where key
is ...
, like this:
╔════════╤═══════╤═══════╤════════════╗
║ linenr │ group │ key │ value ║
╠════════╪═══════╪═══════╪════════════╣
║ 2 │ 1 │ tags │ a ║
║ 3 │ 1 │ ... │ b ║
║ 4 │ 2 │ title │ The Title ║
║ 5 │ 3 │ note │ this is ║
║ 6 │ 3 │ ... │ an EXAMPLE ║
║ 8 │ 4 │ title │ over ║
║ 9 │ 4 │ ... │ three ║
║ 10 │ 4 │ ... │ lines ║
║ 11 │ 5 │ about │ grouping ║
╚════════╧═══════╧═══════╧════════════╝
I tried to do this with windows / partitions and the tabibitosan pattern, but haven't been able to come up with anything working; also, using at the preceding row with lag()
doesn't help where there are multiple consecutive lines with ...
. In a spreadsheet this is an extremely easy thing to do but in SQL I can seemingly not refer to the previous row of the current query, can I?