Re: Counting the number of repeated phrases in a column - Mailing list pgsql-general

From Merlin Moncure
Subject Re: Counting the number of repeated phrases in a column
Date
Msg-id CAHyXU0zndrADTUOMfsLwWfuARGiXxcgbu581b-iqyVJnqvUFQQ@mail.gmail.com
Whole thread Raw
In response to Re: Counting the number of repeated phrases in a column  (Merlin Moncure <mmoncure@gmail.com>)
Responses Re: Counting the number of repeated phrases in a column  (Rob Sargent <robjsargent@gmail.com>)
Re: Counting the number of repeated phrases in a column  (Shaozhong SHI <shishaozhong@gmail.com>)
List pgsql-general
On Wed, Jan 26, 2022 at 5:23 PM Merlin Moncure <mmoncure@gmail.com> wrote:
>
> with s as (select 'Hello World Hello World' as sentence)
> select
>   phrase,
>   array_upper(string_to_array((select sentence from s), phrase), 1) -
> 1 as occurrances
> from
> (
>   select array_to_string(x, ' ') as phrase
>   from
>   (
>     select distinct v[a:b]  x
>     from regexp_split_to_array((select sentence from s), ' ') v
>     cross join lateral generate_series(1, array_upper(v, 1)) a
>     cross join lateral generate_series(a + 1, array_upper(v, 1)) b
>   ) q
> ) q;

Simplified to:
select distinct array_to_string(v[a:b], ' ') phrase, count(*) as occurrences
from regexp_split_to_array('Hello World Hello World', ' ') v
cross join lateral generate_series(1, array_upper(v, 1)) a
cross join lateral generate_series(a + 1, array_upper(v, 1)) b
group by 1;

         phrase          │ occurances
─────────────────────────┼────────────
 World Hello             │          1
 Hello World Hello       │          1
 Hello World             │          2
 Hello World Hello World │          1
 World Hello World       │          1

merlin



pgsql-general by date:

Previous
From: Игорь Выскорко
Date:
Subject: Re: Broken logical replication
Next
From: Rob Sargent
Date:
Subject: Re: Counting the number of repeated phrases in a column