Re: converting a N rows table to a 1 row table ?

From: Chris Curvey <chris(at)chriscurvey(dot)com>
To: Arup Rakshit <aruprakshit(at)rocketmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: converting a N rows table to a 1 row table ?
Date: 2014-07-03 12:53:53
Message-ID: CADfwSsCN_YSvQmpNWaMrHousq-4ieKeH03Ph5D20NNXkvR1Mqw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Jul 3, 2014 at 8:30 AM, Arup Rakshit <aruprakshit(at)rocketmail(dot)com>
wrote:

> Hi,
>
> One query is producing the below table :-
>
> answer | count | avg
> a1 3 14
> a2 2 10
>
> How to convert this to a single row table ?
>
> count | avg_a1 | avg_a2
> 5 14 10
>
> Regards,
> Arup Rakshit
>

select count(*)
, avg(case when answer='a1' then whatever_value else null end) as avg_a1
, avg(case when answer='a2' then whatever_value else null end) as avg_a2
from my_table

--
I asked the Internet how to train my cat, and the Internet told me to get a
dog.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message T 2014-07-03 13:00:22 What kinds of simple expressions need a snapshot?
Previous Message Pujol Mathieu 2014-07-03 12:46:41 Re: converting a N rows table to a 1 row table ?