Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 09B156328E5 for ; Tue, 7 Sep 2010 23:23:52 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 34805-10 for ; Wed, 8 Sep 2010 02:23:44 +0000 (UTC) Received: from news.hub.org (news.hub.org [200.46.204.72]) by mail.postgresql.org (Postfix) with ESMTP id AE90A632898 for ; Tue, 7 Sep 2010 23:23:44 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.14.4/8.14.4) with ESMTP id o882Ngkb007608 for ; Tue, 7 Sep 2010 23:23:42 -0300 (ADT) (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.14.4/8.14.4/Submit) id o882EBur003173 for pgsql-sql@postgresql.org; Tue, 7 Sep 2010 23:14:11 -0300 (ADT) (envelope-from news) From: Lew X-Newsgroups: pgsql.sql Subject: Re: is there a distinct function for comma lists ? Date: Tue, 07 Sep 2010 22:14:31 -0400 Organization: albasani.net Lines: 53 Message-ID: References: <4C862773.2070900@gmx.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit In-Reply-To: <4C862773.2070900@gmx.net> Cancel-Lock: sha1:fvLjANvZJDB0QxBgXRfem+XB9Eo= User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.11) Gecko/20100713 Thunderbird/3.0.6 Injection-Info: news.albasani.net; logging-data="OEuFE2h7AXLgCyppbnR/bGr3ezsjN7Udp3KGCDHW4oVNP7diyTMsteEb7ay/Ux4gIO/RXb8KGdiiFmSpRSZrYckKB+ZVDSxGVFB5lAC+rGU3SUbSbg3lWFjZMnOF838d"; mail-complaints-to="abuse@albasani.net" To: pgsql-sql@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201009/16 X-Sequence-Number: 34547 On 09/07/2010 07:52 AM, Andreas wrote: > Hi, > is there a distinct function for comma separated lists ? > > I sometimes need to update tables where I got a set of IDs, like: > > update mytable > set someattribute = 42 > where mytable.id in > ( 1, 2, 3, 5, 7, 11, 3, 6, 13, 13, 3, 11 ... ) > > So there are double entries in the list but in this case its just > overhead but no problem. > > But for calculated values this would not allways be desirable. > > update mytable > set someattribute = someattribute + 1 > where mytable.id in > ( 1, 2, 3, 5, 7, 11, 3, 6, 13, 13, 3, 11 ... ) > > How could I get a distinct list? Those lists can have 2000-3000 IDs > sometimes. > > One solution was as follows but perhaps there is something more elegant? > > update mytable > set someattribute = someattribute + 1 > where mytable.id in > ( select distinct id from mytable where id in ( 1, 2, 3, 5, 7, 11, 3, 6, > 13, 13, 3, 11 ... ) ) I am not clear on what you're asking here. From what you say, there's nothing to do. The two forms of the SQL you show have the same result. The fact that 11 or 13 or whatever appear in the IN list more than once doesn't affect the result of the query; 13 is in the IN list no matter how many times (> 0) that 13 appears in the IN list. So a row from mytable with id=13 is selected regardless. It's not like the row will be selected more than once. From the manual: 'The result of IN is "true" if any equal subquery row is found.' It's still true of more than one equal subquery row is found. It's not true multiple times, it's just true. If mytable.id is not unique, then every row with that value will be selected, but adding DISTINCT to the IN list won't change that either. -- Lew