Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hvAL5-0007rp-Ns for pgsql-general@arkaria.postgresql.org; Wed, 07 Aug 2019 01:02:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hvAL4-0004Eh-Fe for pgsql-general@arkaria.postgresql.org; Wed, 07 Aug 2019 01:02:02 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hvAL4-0004DX-5G for pgsql-general@lists.postgresql.org; Wed, 07 Aug 2019 01:02:02 +0000 Received: from squeakycode.net ([96.126.114.205]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hvAL1-0002mC-TS for pgsql-general@lists.postgresql.org; Wed, 07 Aug 2019 01:02:01 +0000 Received: from rogue.localdomain (unknown [204.141.215.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: andy) by squeakycode.net (Postfix) with ESMTPSA id DA79E22829 for ; Tue, 6 Aug 2019 20:01:55 -0500 (CDT) Subject: Re: Guidance needed on an alternative take on common prefix SQL To: pgsql-general@lists.postgresql.org References: From: Andy Colson Message-ID: Date: Tue, 6 Aug 2019 20:01:54 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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