Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id EAE43B5DBD0 for ; Mon, 15 Aug 2011 10:08:13 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 68578-02 for ; Mon, 15 Aug 2011 13:08:07 +0000 (UTC) X-Greylist: delayed 00:07:27.469228 by SQLgrey-1.7.6 Received: from forward6.mail.yandex.net (forward6.mail.yandex.net [77.88.60.125]) by mail.postgresql.org (Postfix) with ESMTP id 37D3CB5DBBE for ; Mon, 15 Aug 2011 10:08:07 -0300 (ADT) Received: from web108.yandex.ru (web108.yandex.ru [77.88.61.9]) by forward6.mail.yandex.net (Yandex) with ESMTP id 8049CF81A36 for ; Mon, 15 Aug 2011 17:00:37 +0400 (MSD) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1313413237; bh=MTwZq8IqLIxuyTTh33jwzlu26An3D0JpXLk/EfzGkZE=; h=From:To:In-Reply-To:References:Subject:MIME-Version:Message-Id: Date:Content-Transfer-Encoding:Content-Type; b=fFLI8C8CjcsWb9z3GBAlKy7YkuenjLGkSp402vmiqlgxQZR7+tc9q08douMa8lQIz b2XXO086wDSQsH5A6skxDBUvZaZ7cm+8uf0mqpBt9hSArjXMGZSEEcxbI3Leo0zcqg aHqEQqPS1w59MzdtNy0qmKqyg2aHGVkg/SyDNn64= Received: from localhost (localhost.localdomain [127.0.0.1]) by web108.yandex.ru (Yandex) with ESMTP id 720DC478036 for ; Mon, 15 Aug 2011 17:00:37 +0400 (MSD) X-Yandex-Spam: 1 Received: from [89.179.29.67] ([89.179.29.67]) by web108.yandex.ru with HTTP; Mon, 15 Aug 2011 17:00:37 +0400 From: msi77 To: pgsql-sql@postgresql.org In-Reply-To: References: Subject: Re: Calculate the ratio MIME-Version: 1.0 Message-Id: <765121313413237@web108.yandex.ru> Date: Mon, 15 Aug 2011 17:00:37 +0400 X-Mailer: Yamail [ http://yandex.ru ] 5.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=utf-8 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.789 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_ENVFROM_END_DIGIT=0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201108/45 X-Sequence-Number: 35713 Hi, Try this select gridyear, gwcode, sum(case when cf=1 then area else o end) as cf1, sum(case when cf=0 then area else 0 end) as cf0 from table group by gridyear, gwcode http://sql-ex.ru/help/select8.php#case 15.08.2011, 16:21, "Andreas Forø Tollefsen" : > Hi all, > > This is probably quite simple. I want to calculate the ratio of each country (gwcode) which experiences a 1 in one column (cf). > > Structure: > > gid; gridyear; gwcode; area; cf > > I want to select the SUM(area) WHERE cf = 1 into one column, and SUM(area) WHERE cf = 0 into another column and group by gridyear and gwcode. > Then i can see the ratio of the area of each country affected by cf. > > Any suggestions? > > Thanks. > > A.