Re: aggregate functions in "SELECT"

From: Gerald Cheves <gcheves(at)verizon(dot)net>
To: Vik Fearing <vik(dot)fearing(at)dalibo(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: aggregate functions in "SELECT"
Date: 2013-12-03 22:54:18
Message-ID: 529E611A.6050907@verizon.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Thanks so much, Vik.

- Gerald

On 12/3/2013 5:08 PM, Vik Fearing wrote:
> On 12/03/2013 10:44 PM, Gerald Cheves wrote:
>> Dear Colleagues,
>>
>> How can I use the COUNT variable and the COL_YES variable to calculate
>> a percentage COL_YES/COUNT*100?
>>
>> This operation isn't allowed in the "SELECT" statement.
> You'll need to use a superquery. See below.
>
>> Select g.STATE,
>> g.COMPANY,
>> g.MODEL,
>> count(g.MODEL) as COUNT,
>> coalesce(sum(case when COLORS = 'Yes' then 1 else 0 end),0) as
>> COL_YES
>> from gentech_12_13 as g
>> where MODEL = '0387' and COMPANY = 'ACME'
>> group by g.STATE, g.COMPANY, g.MODEL
>> ;
>>
> SELECT state, company, model, count, col_yes, 100.0*col_yes/count
> FROM (
> Select g.STATE,
> g.COMPANY,
> g.MODEL,
> count(g.MODEL) as COUNT,
> coalesce(sum(case when COLORS = 'Yes' then 1 else 0 end),0)
> as COL_YES
> from gentech_12_13 as g
> where MODEL = '0387' and COMPANY = 'ACME'
> group by g.STATE, g.COMPANY, g.MODEL
> ) q;
>

--
siamo arrivati sani e salvi

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Andy Kerslake 2013-12-05 14:00:19 Trying to set up synchronous replication. fe_sendauth: no password supplied
Previous Message Vik Fearing 2013-12-03 22:08:01 Re: aggregate functions in "SELECT"