Re: Calculate the ratio

From: msi77 <msi77(at)yandex(dot)ru>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Calculate the ratio
Date: 2011-08-15 13:00:37
Message-ID: 765121313413237@web108.yandex.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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" <andreasft(at)gmail(dot)com>:
> 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.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message David Johnston 2011-08-15 13:08:20 Re: Calculate the ratio
Previous Message Andreas Forø Tollefsen 2011-08-15 12:21:34 Calculate the ratio