Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nD8H4-0006em-PO for pgsql-general@arkaria.postgresql.org; Thu, 27 Jan 2022 17:09:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nD8H3-0004F7-DJ for pgsql-general@arkaria.postgresql.org; Thu, 27 Jan 2022 17:09:29 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nD8H3-0004Ey-1C for pgsql-general@lists.postgresql.org; Thu, 27 Jan 2022 17:09:29 +0000 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nD8H0-0005js-Lk for pgsql-general@lists.postgresql.org; Thu, 27 Jan 2022 17:09:27 +0000 Received: by mail-pl1-x634.google.com with SMTP id c3so3031730pls.5 for ; Thu, 27 Jan 2022 09:09:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to; bh=DDQ9lfgccV6eyqk2b1SRUsjVWlUfZKGZ9pD9qo3Lw2k=; b=J9OuP1tjq39Vts01/qHisveVlzeT3e0BDMAmIFXCb4nDfVFI7as5o46AMp1xA/qMxN 0TC1OeHVtj/vHEajsUnAy+s8nWjhfNqOJ3TOxop3l0DJxcTvO926NQaf2hfHAELWbFx4 +qEhyLpJv/Z8jhhjBv8FPNBbcRD1k7UUTxSx3qx48/6y62rRyL91+tdaT/71GbMt/qhk c8Aen2XOnbciQtkgDIFF4256mLRDulF54RQWPjaZ/rtn9rib7+qvNYlKNjKaeRyB+lbO sSak2MFDw+wWvGxg6PXqiThVCaYSLJa4qkbyBTaZxClunj36qSW3KQdRQdGKzMxeaR4N nh8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to; bh=DDQ9lfgccV6eyqk2b1SRUsjVWlUfZKGZ9pD9qo3Lw2k=; b=mIfwrjblC3DOAWRKTgB68nDomGK7BEG7mxIbQCjDbXC9NaedrZziCdEXD5rAGqWeQY t8CMkVXKjg//1QeAq2cx/cri8Ya0g4bqdx8BS87+Jnq6Ub9asHB0Z7I07/6lYi/iVFwh GL/cH0JX2YZ7p3qBjU+d1rYEqto/y0ZNkSHD1BWxiTDNrQmyphq1ELOHEaDuXufm8zDO +6WpJvO6BckReA3Bc5oir+XjWi7PvWZQJoYPW+GODa+sVnhJxVcxVdZrzWCqvwXmovlz bLD0F0SukTAg1O4G8ZdPNoopH3fzHU61oTzGQXF20rijwiDR3ATboO6PjOiXfoKyZ+mw tRig== X-Gm-Message-State: AOAM532MTcYvgFQnidzZkaKLhbs2BBVV2Iz74QS53hdrsb3SmHSB2Yrn rqXXIWysF4IvAvO3N7K6wEdYj3D0kG8= X-Google-Smtp-Source: ABdhPJzs9TBVPqQBtlCxJofxyHbmQOwSsDvnJzf2Ki9SAurjYijjbiXF0oXKYB3vDobLEn0WU0b8zg== X-Received: by 2002:a17:90a:b018:: with SMTP id x24mr5034192pjq.91.1643303365513; Thu, 27 Jan 2022 09:09:25 -0800 (PST) Received: from [10.0.0.18] (c-73-63-59-118.hsd1.ut.comcast.net. [73.63.59.118]) by smtp.gmail.com with ESMTPSA id me4sm7763961pjb.26.2022.01.27.09.09.24 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 27 Jan 2022 09:09:25 -0800 (PST) Content-Type: multipart/alternative; boundary="------------ELlERWCLVYDcK9dq9cE5xR00" Message-ID: <22841e5f-99b3-c507-5610-d7962ae69e15@gmail.com> Date: Thu, 27 Jan 2022 10:09:24 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: Counting the number of repeated phrases in a column Content-Language: en-CA To: pgsql-general@lists.postgresql.org References: From: Rob Sargent In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------ELlERWCLVYDcK9dq9cE5xR00 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 1/27/22 10:03, Merlin Moncure wrote: > On Wed, Jan 26, 2022 at 5:23 PM Merlin Moncure 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 --------------ELlERWCLVYDcK9dq9cE5xR00 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
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

--------------ELlERWCLVYDcK9dq9cE5xR00--