I'm doing some data analysis in PostgreSQL 10.6, based on this demo setup:
create table history (
registered_unix int,
requested_unix int,
alias character varying(255)
);
INSERT INTO history VALUES
(1537841388, 1537878224, '3')
, (1538093202, 1538095740, '1')
, (1538093186, 1538095740, '3')
, (1538105501, 1538107039, '2')
, (1538105501, 1538107039, '4')
, (1538205007, 1538242243, '2')
, (1538205012, NULL , '1')
, (1538105501, NULL , '1')
, (1538205007, NULL , '3')
, (1538105501, NULL , '3')
, (1538040863, NULL , '3')
, (1537985996, NULL , '3')
, (1538205007, NULL , '4')
, (1538093186, NULL , '4')
, (1538205301, NULL , '5')
, (1538105501, NULL , '5')
, (1538093186, NULL , '5');
I'm trying to count the number of consecutive alias
rows where requested_unix
is null, but only for the first occurrence and where the consecutive occurrences are greater or equal to 2.
The data is sorted by alias, then registered_unix DESC:
Desired result:
Alias 1: Included b/c its 2 most recent "requested_unix" values are null.
Alias 2: Skipped. Requested unix has a value on its most recent row
Alias 3: Included b/c its 2 most recent "requested_unix" values are null. The older ones are discarded.
Alias 4: Skipped. Only has 1 most recent "requested_unix" value with null. 2nd row has a value.
Alias 5: Included. 3 consecutive most recent values are null.
There are all kind of tickets with counting consecutive occurrences or finding the first occurrence, but it seems I want to combine the two approaches.