converting a N rows table to a 1 row table ?

Started by Arup Rakshitalmost 12 years ago4 messagesgeneral
Jump to latest
#1Arup Rakshit
aruprakshit@rocketmail.com

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

#2Pujol Mathieu
mathieu.pujol@realfusio.com
In reply to: Arup Rakshit (#1)
Re: converting a N rows table to a 1 row table ?

Le 03/07/2014 14:30, Arup Rakshit a �crit :

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

Hi,
Could you have multiple row with same answer ?
If I understand you one row and N + 1 column where N is the number of
answer ?
Regards

#3Chris Curvey
chris@chriscurvey.com
In reply to: Arup Rakshit (#1)
Re: converting a N rows table to a 1 row table ?

On Thu, Jul 3, 2014 at 8:30 AM, Arup Rakshit <aruprakshit@rocketmail.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.

#4Arup Rakshit
aruprakshit@rocketmail.com
In reply to: Pujol Mathieu (#2)
Re: converting a N rows table to a 1 row table ?

Hi,
Could you have multiple row with same answer ?
If I understand you one row and N + 1 column where N is the number
of answer ?
Regards
You are right. Current it is fixed 2 answer. It means N = 2.