pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Combining values in a column
2+ messages / 2 participants
[nested] [flat]

* Combining values in a column
@ 2005-04-15 09:22 Stephen Quinney <stephen.quinney@computing-services.oxford.ac.uk>
  2005-04-15 10:12 ` Re: Combining values in a column Sean Davis <sdavis2@mail.nih.gov>
  0 siblings, 1 reply; 2+ messages in thread

From: Stephen Quinney @ 2005-04-15 09:22 UTC (permalink / raw)
  To: pgsql-sql

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







^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Combining values in a column
  2005-04-15 09:22 Combining values in a column Stephen Quinney <stephen.quinney@computing-services.oxford.ac.uk>
@ 2005-04-15 10:12 ` Sean Davis <sdavis2@mail.nih.gov>
  0 siblings, 0 replies; 2+ messages in thread

From: Sean Davis @ 2005-04-15 10:12 UTC (permalink / raw)
  To: Stephen Quinney <stephen.quinney@computing-services.oxford.ac.uk>; +Cc: pgsql-sql

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
>




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2005-04-15 10:12 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-04-15 09:22 Combining values in a column Stephen Quinney <stephen.quinney@computing-services.oxford.ac.uk>
2005-04-15 10:12 ` Sean Davis <sdavis2@mail.nih.gov>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox