PD
Size: a a a
PD
ДВ
PU
PU
ДВ
PU
ДВ
AP
PU
PU
AP
PU
PU
PU
PU
PD
with tmp as (
select 1 as user_id, 'swamp' as level, 'lost' as result union all
select 1, 'swamp', 'won' union all
select 1, 'swamp', 'won' union all
select 2, 'desert', 'won' union all
select 2, 'swamp', 'list' union all
select 3, 'desert', 'lost' union all
select 3, 'swamp', 'won'
)
, process_table as (
select
result,
string_agg(level, ' > ') over (partition by user_id rows between current row and 1 following ) as process
from tmp
)
select result as first_result, process, count(*) as N
from process_table
where array_length(split(process, " > ")) > 1
group by result, process
order by count(*) desc
PU
PD
with tmp as (
select 1 as user_id, 'swamp' as level, 'lost' as result union all
select 1, 'swamp', 'won' union all
select 1, 'swamp', 'won' union all
select 2, 'desert', 'won' union all
select 2, 'swamp', 'list' union all
select 3, 'desert', 'lost' union all
select 3, 'swamp', 'won'
)
, process_table as (
select
result,
level as level_one,
lead(level) over (partition by user_id order by user_id) as level_two
from tmp
)
select
*, count(*) as N
from process_table
where level_two is not null
group by result, level_one, level_two
order by result, level_one, level_two;
IS