pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andy Colson <andy@squeakycode.net>
To: pgsql-general@lists.postgresql.org
Subject: Re: Guidance needed on an alternative take on common prefix SQL
Date: Tue, 6 Aug 2019 20:01:54 -0500
Message-ID: <ab7e79db-b24b-0ef6-efa1-42e6440770c7@squeakycode.net> (raw)
In-Reply-To: <XwheTGk8js0Qqp7RkhH0x_X-rOHSjKPobxXliWeDyyAnYMPSshnUotu9GJ8RZcCjGQY7FDWf0oS8JIi0HYbnBhwGNG2igO2-yHUM-TjQ8SA=@protonmail.ch>
References: <XwheTGk8js0Qqp7RkhH0x_X-rOHSjKPobxXliWeDyyAnYMPSshnUotu9GJ8RZcCjGQY7FDWf0oS8JIi0HYbnBhwGNG2igO2-yHUM-TjQ8SA=@protonmail.ch>

On 8/6/19 6:25 PM, Laura Smith wrote:
> Hi,
> 
> I've seen various Postgres examples here and elsewhere that deal with the old common-prefix problem (i.e. "given 1234 show me the longest match").
> 
> I'm in need of a bit of guidance on how best to implement an alternative take.  Frankly I don't quite know where to start but I'm guessing it will probably involve CTEs, which is an area I'm very weak on.
> 
> So, without further ado, here's the scenario:
> 
> Given an SQL filtering query output that includes the following column:
> 87973891
> 87973970
> 87973971
> 87973972
> 87973973
> 87973975
> 87973976
> 87973977
> 87973978
> 87973979
> 8797400
> 
> The final output should be further filtered down to:
> 87973891
> 8797397
> 8797400
> 
> i.e. if $last_digit is present 0–9 inclusive, recursively filter until the remaining string is all the same (i.e. in this case, when $last_digit[0-9] is removed, 8797397 is the same).
> 
> So, coming back to the example above:
> 8797397[0-9] is present
> so the "nearest common" I would be looking for is 8797397 because once [0-9] is removed, the 7 is the same on the preceeding digit.
> 
> The other two rows ( 87973891 and 8797400) are left untouched because $last_digit is not present in [0-9].
> 
> Hope this question makes sense !
> 
> Laura
> 
> 


Hows this?

select distinct
   case cc
      when 1 then num
      else left(num,-1)
   end
from (
    select
       num,
       (select count(*) as cc from numbers n2 where left(n2.num, -1) = left(numbers.num, -1))
    from numbers
) as tmpx ;


-Andy





view thread (4+ messages)  latest in thread

Message-ID: <ab7e79db-b24b-0ef6-efa1-42e6440770c7@squeakycode.net>
Permalink:  ../ab7e79db-b24b-0ef6-efa1-42e6440770c7@squeakycode.net/
Also on:    postgresql.org/message-id/ab7e79db-b24b-0ef6-efa1-42e6440770c7@squeakycode.net

 · 

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: andy@squeakycode.net
  Subject: Re: Guidance needed on an alternative take on common prefix SQL
  In-Reply-To: <ab7e79db-b24b-0ef6-efa1-42e6440770c7@squeakycode.net>
  References: <XwheTGk8js0Qqp7RkhH0x_X-rOHSjKPobxXliWeDyyAnYMPSshnUotu9GJ8RZcCjGQY7FDWf0oS8JIi0HYbnBhwGNG2igO2-yHUM-TjQ8SA=@protonmail.ch> <ab7e79db-b24b-0ef6-efa1-42e6440770c7@squeakycode.net>

* 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