Need query to separate rows by one field's value

From: Jeff Boes <jboes(at)nexcerpt(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Need query to separate rows by one field's value
Date: 2003-11-18 18:41:31
Message-ID: 021a9dcef9be53a70a73251b8fe9080a@news.teranews.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Here's a query challenge for you. Given:

table foo (
a integer primary key,
b integer check (b > 0))

with a limited number of rows (say, <= 10). Values in column "a" are
unique (obviously). Values in column "b" are not.

I want to find a set of values for "a" such that the sum of "b" values
is as close to one-half of the total sum of "b" over all rows. That is,

select 2.0*sum(b) from foo
where a in (...);

would be approximately the same as

select sum(b) from foo;

Likewise,

select 2.0*sum(b) from foo
where a NOT in (...);

would be approximately that value, too.

--
Jeff Boes vox 269.226.9550 ext 24
Database Engineer fax 269.349.9076
Nexcerpt, Inc. http://www.nexcerpt.com
...Nexcerpt... Extend your Expertise

Browse pgsql-sql by date

  From Date Subject
Next Message Paul Ganainm 2003-11-18 22:05:00 Arrays - a good idea?
Previous Message Troels Arvin 2003-11-18 15:06:36 Re: Quota query with decent performance?