Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eO6ew-00030z-TP for pgsql-sql@arkaria.postgresql.org; Sun, 10 Dec 2017 18:49:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eO6ew-0006DJ-6R for pgsql-sql@arkaria.postgresql.org; Sun, 10 Dec 2017 18:49:06 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eO6ev-0006DA-U1 for pgsql-sql@lists.postgresql.org; Sun, 10 Dec 2017 18:49:06 +0000 Received: from mail.i-mark.de ([188.138.104.222]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eO6es-0006Tm-Px for pgsql-sql@lists.postgresql.org; Sun, 10 Dec 2017 18:49:04 +0000 Received: from [192.168.222.106] (p54B09956.dip0.t-ipconnect.de [84.176.153.86]) by mail.i-mark.de (Postfix) with ESMTPSA id 89CD71581993 for ; Sun, 10 Dec 2017 19:48:54 +0100 (CET) Subject: Re: window function ? To: pgsql-sql@lists.postgresql.org References: <5a2d7e15.841a1c0a.ad030.7447@mx.google.com> From: Andreas Kretschmer Message-ID: Date: Sun, 10 Dec 2017 19:48:58 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <5a2d7e15.841a1c0a.ad030.7447@mx.google.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Am 10.12.2017 um 19:33 schrieb Olivier Leprêtre: > > Hi, > > I have a table containing sort of boxes in different categories > described by three columns categorie/box/count > > In each categorie, I want to associate each box with the count of the > others (sum of counts of this categorie but not the current one). > > As an example : > > cat     box     count > > cat1    box21 2 > > cat1    box23 6 > > cat1    box34 1 > > cat1    box37 3 > > cat3    box45 12 > > cat3    box62 2 > > cat3    box89 7 > > cat3    box12 9 > > cat3    box28 10 > > cat8    box02 10 > > cat8    box87 2 > > cat8    box46 3 > > will return > > cat1    box21 2        10 (6+1+3) => 2 not added > > cat1    box23 6        6 (2+1+3) => 6 not added > > cat1    box34 1        11 (2+6+3) => 1 not added > > cat1    box37 3        9 (2+6+1) => 3 not added > > cat3    box45 12       28 (2+7+9+10) > > cat3    box62 2        38 (12+7+9+10) > > cat3    box89 7        33 (12+2+9+10) > > cat3    box12 9        31 (12+2+7+10) > > cat3    box28 10       30 (12+2+7+9) > > cat8    box02 10       5 (2+3) > > cat8    box87 2        13 (10+3) > > cat8    box46 3        12 (10+2) > > I searched thru lateral and window functions but didn't manage to do that. > > > > > test=*# select * from boxes ;  cat  |  box  | count ------+-------+-------  cat1 | box21 |     2  cat1 | box23 |     6  cat1 | box34 |     1  cat1 | box37 |     3  cat3 | box45 |    12  cat3 | box62 |     2  cat3 | box89 |     7  cat3 | box12 |     9  cat3 | box28 |    10  cat8 | box02 |    10  cat8 | box87 |     2  cat8 | box46 |     3 (12 Zeilen) test=*# select cat, box, sum(count) over (partition by cat) - count from boxes;  cat  |  box  | ?column? ------+-------+----------  cat1 | box21 |       10  cat1 | box23 |        6  cat1 | box34 |       11  cat1 | box37 |        9  cat3 | box45 |       28  cat3 | box62 |       38  cat3 | box89 |       33  cat3 | box12 |       31  cat3 | box28 |       30  cat8 | box02 |        5  cat8 | box87 |       13  cat8 | box46 |       12 (12 Zeilen) test=*# helps that? Regards, Andreas -- 2ndQuadrant - The PostgreSQL Support Company. www.2ndQuadrant.com