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 1hvWQD-00015i-0j for pgsql-general@arkaria.postgresql.org; Thu, 08 Aug 2019 00:36:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hvWQB-0000fY-Gw for pgsql-general@arkaria.postgresql.org; Thu, 08 Aug 2019 00:36:47 +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 1hvWQA-0008UL-Vd for pgsql-general@lists.postgresql.org; Thu, 08 Aug 2019 00:36:47 +0000 Received: from mail-pg1-x542.google.com ([2607:f8b0:4864:20::542]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hvWQ3-0002E2-Vr for pgsql-general@lists.postgresql.org; Thu, 08 Aug 2019 00:36:46 +0000 Received: by mail-pg1-x542.google.com with SMTP id l21so43044602pgm.3 for ; Wed, 07 Aug 2019 17:36:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=QkqaB1l2B8FMaNq0iJ8kmIQ348IAereI58mPJuQixt0=; b=luDUesFWWoN1XPWRXLlShFs1FQeYmvsj7SBQ/i+KpG4V6N+aS60AYAf65ypJNW2CZY BSW8SyMRO8Nu8Mm4M67EB1poGuRIJ++s6t/BkXRZUWJeRxNJ1xtncjD6YtQNvVAYVW8j 5K0oYQTy0Qf0oO93iMxJAh7JbYwadLqyEAU9czn4nPddf/Hd/fB+cuIJ1YvDj7DSLe6j 18vuqBehgoh3VQ6xLqylfIRaA4MzsqzZp9JMliGofdn4dUOWkgNAUJanKQ9btazCUpRj F/2Mfbgha8eSoG+rqOCr6OdMDwfb5iEyb4Wz1BKLmxF1ONaJ/Dr6g1aNCxuLowQ4B0c2 seKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=QkqaB1l2B8FMaNq0iJ8kmIQ348IAereI58mPJuQixt0=; b=BUSXKe59SIeZzSvND3rNApvPLxuCABg6tqaeUN0Ac06Z9wL+SXjkd1cHlrZ9zNAH0o vfYunxbWNu3qiZhHrv+lxB6uRS72DTf8EGDCJbUuJMkT5vAdIPHSmv17FGnCCTc4sPli wdVEc13IrjA5uqK0YXKqdCee+j0+7Ypb9cenFFASY/vwdAi8xQ8QYWVhOT0NYWn7ZbIR K17Sur/e39jky5eAPt1bs/ZrJhjknWC2pfLdPNO0VuUoy5SlnICeAmFP2beTlo+1quMz 9LpbvEzB7DFtXD+wbwO7gPD6oA/iGCjiBOdwuQQSmKz0M6DLdUHfra3CACBvMleEP259 bChw== X-Gm-Message-State: APjAAAUZIYfydj9scnXAvynYdqKR6dtuVB5ub43zppJUFh3DrJTFrLoi 9YhA73y/xgRyWIWfswHEO24Y8c0R X-Google-Smtp-Source: APXvYqwMvQo0t8BZcaCI5KXTyKtoybxND/1EUZzMMQdfp7x9g2oMnvyeg4LYQCFLb+ujtUFTjG1SWw== X-Received: by 2002:a17:90a:ad93:: with SMTP id s19mr1150312pjq.36.1565224597264; Wed, 07 Aug 2019 17:36:37 -0700 (PDT) Received: from [10.104.134.41] ([155.100.47.1]) by smtp.gmail.com with ESMTPSA id 124sm95293617pfw.142.2019.08.07.17.36.36 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Aug 2019 17:36:36 -0700 (PDT) Subject: Re: Guidance needed on an alternative take on common prefix SQL To: pgsql-general@lists.postgresql.org References: From: Rob Sargent Message-ID: <8a7d34af-73d0-3a7f-f199-3fef64b46fca@gmail.com> Date: Wed, 7 Aug 2019 18:36:35 -0600 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-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 8/7/19 3:36 AM, Laura Smith wrote: > On Wednesday, August 7, 2019 2:01 AM, Andy Colson wrote: > >> 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 > > > Hi Andy, > > That looks supremely clever ! > > I have just done a quick test and looks like it works as intended. Will do some more thorough testing with a larger dataset in due course. > > Thank you very much indeed > > Laura > > If the target field is really an integer type and you have lots of rows you might be better off with arithmetic functions. create table short as select id/10 as base, array_agg(mod(id,10)) as odds from head group by base; select * from short;   base   |        odds ---------+---------------------   879740 | {0}  8797389 | {1}  8797397 | {0,1,2,3,5,6,7,8,9} (3 rows)  select case when array_length(odds,1) = 1 then 10*base + odds[1] else base end from short;    base ----------   8797400  87973891   8797397 (3 rows)