pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rob Sargent <robjsargent@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Counting the number of repeated phrases in a column
Date: Thu, 27 Jan 2022 10:09:24 -0700
Message-ID: <22841e5f-99b3-c507-5610-d7962ae69e15@gmail.com> (raw)
In-Reply-To: <CAHyXU0zndrADTUOMfsLwWfuARGiXxcgbu581b-iqyVJnqvUFQQ@mail.gmail.com>
References: <CA+i5Jwa5j2m4q6ARt_1U=bWz5=cRqPqqSJ38mYwaP2XF0k_WNg@mail.gmail.com>
	<CAHyXU0x501igQ2x_83wTfsL1pg0e9aRYwr0ScxSR+w95r8CJPg@mail.gmail.com>
	<CAHyXU0zndrADTUOMfsLwWfuARGiXxcgbu581b-iqyVJnqvUFQQ@mail.gmail.com>

On 1/27/22 10:03, Merlin Moncure wrote:
> 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
>
>
And since we're looking for repeated phrases maybe add

    having count(*) > 1

view thread (23+ messages)  latest in thread

Message-ID: <22841e5f-99b3-c507-5610-d7962ae69e15@gmail.com>
Permalink:  ../22841e5f-99b3-c507-5610-d7962ae69e15@gmail.com/
Also on:    postgresql.org/message-id/22841e5f-99b3-c507-5610-d7962ae69e15@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: robjsargent@gmail.com
  Subject: Re: Counting the number of repeated phrases in a column
  In-Reply-To: <22841e5f-99b3-c507-5610-d7962ae69e15@gmail.com>
  References: <CA+i5Jwa5j2m4q6ARt_1U=bWz5=cRqPqqSJ38mYwaP2XF0k_WNg@mail.gmail.com> <CAHyXU0x501igQ2x_83wTfsL1pg0e9aRYwr0ScxSR+w95r8CJPg@mail.gmail.com> <CAHyXU0zndrADTUOMfsLwWfuARGiXxcgbu581b-iqyVJnqvUFQQ@mail.gmail.com> <22841e5f-99b3-c507-5610-d7962ae69e15@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox