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 1k1r1H-0006Oa-1N for pgsql-sql@arkaria.postgresql.org; Sat, 01 Aug 2020 12:53:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k1r1G-0005ov-0Y for pgsql-sql@arkaria.postgresql.org; Sat, 01 Aug 2020 12:53:46 +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 1k1r1F-0005oo-QK for pgsql-sql@lists.postgresql.org; Sat, 01 Aug 2020 12:53:45 +0000 Received: from mail-oo1-xc35.google.com ([2607:f8b0:4864:20::c35]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1k1r1C-0007aL-Ou for pgsql-sql@lists.postgresql.org; Sat, 01 Aug 2020 12:53:44 +0000 Received: by mail-oo1-xc35.google.com with SMTP id t6so6435117ooh.4 for ; Sat, 01 Aug 2020 05:53:42 -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-language:content-transfer-encoding; bh=bVW6QKXWejILJAQANsEBd/g0Pjk1q7w4VK68oz8sF5s=; b=udcEM9wkXJSPSLVlL57Yk/RGCEvPPx0RrBCh8MtVHVGkYsFG4LdhOx5nTM9PHzSygG tgmlr3lLF9HKLaiVTo2FB7SnMQM2neJAOy+yrAT8Ug4AUObXWVcKjct3ic29nU6zVEWW msOmHKCWb3CTwapZjXE1Tc+TgYvAyDWJHwfp6kO67Z4W1kdWAAL7OkSmFYiE5q+7Y8XO 6mo0vVEFA3ffxiAjOuQHSlKvNkHsKbcr9wSNFNj53gsExPX3OPBSPwhvJ+Veis3zwrJX yK0He/UlWMuvj3RUpR6xmmFJJ94YJI9Yd5Djdh5jS+v0Z15Wub+ZekfBiU4hAZgqLN/e Z89Q== 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-language :content-transfer-encoding; bh=bVW6QKXWejILJAQANsEBd/g0Pjk1q7w4VK68oz8sF5s=; b=LC7HtX3uugkbe40Kvjiqa2SFzCyxwtA+CYNW9s5/3pZYDgzJWcUBeh5MAR4IImX4P5 XabOjlvrY81t63GJkPBMYDuP+ACxlQ7B3qahc+EJjOE+luolhKZI76cv1iritFkACVTx dmcNHqQRWgTSfM2ouPb53s7Uwb5ZydmEIEE3+faKuk/srolVfIqqlUVnmyou6rTIzQDE MGm26PirvQk9VJCfqykhY62EsGA0WZpN5ajlUq5ay2296CU3ZXuIb6h9/IFNsgTY/ou/ C1kRM072WTAs1SrW3RCk9L2NXF2v5rNBk159j5r9Nx1WBi6ATVWQs/WwhJ0A2SJuCV3n lCQg== X-Gm-Message-State: AOAM531BkgpejOkYxr9SqjkG8vSMBJ2CU8Ig07e923C2qBJgvYAuLKFh kzogfsC+FSD4voa8FaujqrjJ+37RvjQ= X-Google-Smtp-Source: ABdhPJxU9mb6IbPjRA337YFkdegahDNRQg5EIUNJbaNES+fL0sEy+S1aPgRQRILhCn/58IyIy7VliQ== X-Received: by 2002:a4a:3e4b:: with SMTP id t72mr7159878oot.3.1596286422015; Sat, 01 Aug 2020 05:53:42 -0700 (PDT) Received: from ?IPv6:2601:681:5500:dde0:d075:5015:6f3f:df86? ([2601:681:5500:dde0:d075:5015:6f3f:df86]) by smtp.gmail.com with ESMTPSA id w74sm1970668oif.57.2020.08.01.05.53.39 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 01 Aug 2020 05:53:41 -0700 (PDT) Subject: Re: Group by a range of values To: pgsql-sql@lists.postgresql.org References: From: Rob Sargent Message-ID: Date: Sat, 1 Aug 2020 06:53:38 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-CA Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 8/1/20 6:34 AM, Torsten Grust wrote: > Hi, > > maybe this does the job already (/ is integer division): > > SELECT i, 1 + (i-1) / 3 > FROM   generate_series(1,10) AS i; > > An expression like (i-1) / 3 could, of course, also be used as > partitioning criterion in GROUP BY and/or window functions. > > Cheers, >   —T > > On Sat, Aug 1, 2020 at 2:15 PM Mike Martin > wrote: > > Say I have a field of ints, as for example created by with > ordinality or generate_series, is it possible to group by a range? eq > > 1,2,3,4,5,6,7,8,9,10 > step 3 > so output is > > 1 1 > 2 1 > 3 1 > 4 2 > 5 2 > 6 2 > 7 3 > 8 3 > 9 3 > 10 4 > > thanks > > > > -- > | Torsten Grust > | Torsten.Grust@gmail.com > My version is as follows, the point being that the "grouping" requested is simply an ordering of the step mechanism. Naturally this series is generated in order shown but real data for val likely won't be. test=# with ts as (select generate_series(1,10) as val) select s.val, (s.val /3)+1 as ord from ts as s order by ord; val | ord -----+----- 1 | 1 2 | 1 3 | 2 4 | 2 5 | 2 6 | 3 7 | 3 8 | 3 9 | 4 10 | 4 (10 rows)