X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 2C5C7D1B50A for ; Tue, 18 Nov 2003 18:50:23 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 85772-10 for ; Tue, 18 Nov 2003 14:49:53 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id A99ADD1B4F8 for ; Tue, 18 Nov 2003 14:49:52 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.12.9/8.12.9) with ESMTP id hAIInqNu080112 for ; Tue, 18 Nov 2003 18:49:52 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id hAIIl2MU079600 for pgsql-sql@postgresql.org; Tue, 18 Nov 2003 18:47:02 GMT X-Abuse-Report: abuse@teranews.com Message-ID: <021a9dcef9be53a70a73251b8fe9080a@news.teranews.com> Date: Tue, 18 Nov 2003 18:41:31 GMT Lines: 33 From: Jeff Boes Organization: Nexcerpt, Inc. User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20030925 X-Accept-Language: en-us, en MIME-Version: 1.0 X-Newsgroups: comp.databases.postgresql.sql Subject: Need query to separate rows by one field's value Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200311/217 X-Sequence-Number: 16032 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