Need query to separate rows by one field's value - Mailing list pgsql-sql

From Jeff Boes
Subject Need query to separate rows by one field's value
Date
Msg-id 021a9dcef9be53a70a73251b8fe9080a@news.teranews.com
Whole thread Raw
List 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



pgsql-sql by date:

Previous
From: Troels Arvin
Date:
Subject: Re: Quota query with decent performance?
Next
From: Yasir Malik
Date:
Subject: Re: Addition and subtraction on BIT type