X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 8162B539E1 for ; Fri, 15 Apr 2005 07:12:15 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 44948-07 for ; Fri, 15 Apr 2005 10:12:10 +0000 (GMT) Received: from mx5.net.nih.gov (mailfwd.nih.gov [165.112.130.36]) by svr1.postgresql.org (Postfix) with ESMTP id A32875354D for ; Fri, 15 Apr 2005 07:12:09 -0300 (ADT) Received: from mx5.net.nih.gov (localhost [127.0.0.1]) by mx5.net.nih.gov (8.12.10/8.11.6) with ESMTP id j3FACBas008585 for ; Fri, 15 Apr 2005 06:12:11 -0400 Received: from [128.231.145.14] (holmes.nhgri.nih.gov [128.231.145.14]) by mx5.net.nih.gov (8.12.10/8.11.6) with ESMTP id j3FACBKd008582; Fri, 15 Apr 2005 06:12:11 -0400 In-Reply-To: <20050415092227.GA24135@computing-services.oxford.ac.uk> References: <20050415092227.GA24135@computing-services.oxford.ac.uk> Mime-Version: 1.0 (Apple Message framework v619.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: Content-Transfer-Encoding: 7bit Cc: pgsql-sql@postgresql.org From: Sean Davis Subject: Re: Combining values in a column Date: Fri, 15 Apr 2005 06:12:11 -0400 To: Stephen Quinney X-Mailer: Apple Mail (2.619.2) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.004 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200504/222 X-Sequence-Number: 21115 See below for creating aggregates: http://www.postgresql.org/docs/current/static/xaggr.html But, there is a useful function built-in, bit_and, that does what you want: http://www.postgresql.org/docs/current/static/functions- aggregate.html#FUNCTIONS-AGGREGATE-TABLE create table testint ( myint int ); CREATE TABLE insert into testint values (31); INSERT 428988938 1 insert into testint values (511); INSERT 428988939 1 select bit_and(myint) from testint; bit_and --------- 31 (1 row) On Apr 15, 2005, at 5:22 AM, Stephen Quinney wrote: > I have a query which returns a single column of integers which I want > to combine together with &, "bitwise AND". Basically it's a simple > list of access levels that a user might have and I want to merge the > list into one value. There can be zero, one or more values in the list > so I have to be able to cope with the no-results . > > For example > > access > -------- > 31 > 511 > > Would lead to 31 & 511 which gives 31. > > I guess really this can lead to the more generic question. How do I > apply a general function to combine the values, if I, for example, > wanted to write my own aggregate function like sum? > > Thanks in advance, > > Stephen Quinney > > > > > > ---------------------------(end of > broadcast)--------------------------- > TIP 7: don't forget to increase your free space map settings >