pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jeff Boes <jboes@nexcerpt.com>
To: pgsql-sql@postgresql.org
Subject: Need query to separate rows by one field's value
Date: Tue, 18 Nov 2003 18:41:31 GMT
Message-ID: <021a9dcef9be53a70a73251b8fe9080a@news.teranews.com> (raw)

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




Message-ID: <021a9dcef9be53a70a73251b8fe9080a@news.teranews.com>
Permalink:  ../021a9dcef9be53a70a73251b8fe9080a@news.teranews.com/
Also on:    postgresql.org/message-id/021a9dcef9be53a70a73251b8fe9080a@news.teranews.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jboes@nexcerpt.com
  Subject: Re: Need query to separate rows by one field's value
  In-Reply-To: <021a9dcef9be53a70a73251b8fe9080a@news.teranews.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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