From: David Johnston <polobo@yahoo.com>
To: 'Andreas Forø Tollefsen' <andreasft@gmail.com>
To: pgsql-sql@postgresql.org
Subject: Re: Calculate the ratio
Date: Mon, 15 Aug 2011 09:10:56 -0400
Message-ID: <004401cc5b4c$c5513f20$4ff3bd60$@yahoo.com> (raw)
In-Reply-To: <003901cc5b4c$688042a0$3980c7e0$@yahoo.com>
References: <CAGMz7DnkL2=VxpYqEsqTnMS=MTyDywgu_Z+b79X9ugqn5BgHpg@mail.gmail.com>
<003901cc5b4c$688042a0$3980c7e0$@yahoo.com>
Replace the 1 in the case with area like the msi77 said
From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]
On Behalf Of David Johnston
Sent: Monday, August 15, 2011 9:08 AM
To: 'Andreas Forø Tollefsen'; pgsql-sql@postgresql.org
Subject: Re: [SQL] Calculate the ratio
SELECT cols,
SUM( CASE WHEN cf = 1 THEN 1 ELSE 0 END ) AS sum_1,
SUM ( CASE WHEN cf = 0 THEN 1 ELSE 0 END ) AS sum_0
FROM table
WHERE cf IN (0, 1)
GROUP BY cols
From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]
On Behalf Of Andreas Forø Tollefsen
Sent: Monday, August 15, 2011 8:22 AM
To: pgsql-sql@postgresql.org
Subject: [SQL] Calculate the ratio
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.
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: polobo@yahoo.com, andreasft@gmail.com
Subject: Re: Calculate the ratio
In-Reply-To: <004401cc5b4c$c5513f20$4ff3bd60$@yahoo.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