Re: Not able to understand how to write group by - Mailing list pgsql-general

From David G Johnston
Subject Re: Not able to understand how to write group by
Date
Msg-id 1404328703594-5810279.post@n5.nabble.com
Whole thread Raw
In response to Re: Not able to understand how to write group by  (jared <afonit@gmail.com>)
Responses Re: Not able to understand how to write group by  (Arup Rakshit <aruprakshit@rocketmail.com>)
Re: Not able to understand how to write group by  (Arup Rakshit <aruprakshit@rocketmail.com>)
List pgsql-general
afonit wrote
>> gender    participants       answer1_avg   answer2_avg
>> n                      3                     12                  3
>> m                      5                     4                  12
>> f                       7                    15                    23

Are you sure this is what you want?

Since there are two columns you will have to either use a CASE or a
sub-select to facilitate calculating the values for each of the columns.

SELECT gender, answer1_avg, answer2_avg
FROM (SELECT DISTINCT gender FROM ...) gn
LEFT JOIN (SELECT gender, answer1_avg FROM ...) ans1 USING (gender)
LEFT JOIN (SELECT gender, answer2_avg FROM ...) ans2 USING (gender)

You could also try learning "crosstab" from the "tablefunc" extension:

http://www.postgresql.org/docs/9.3/interactive/tablefunc.html

I do not see how a single "participant count" column will provide a
meaningful piece of data...

David J.






--
View this message in context:
http://postgresql.1045698.n5.nabble.com/Not-able-to-understand-how-to-write-group-by-tp5810250p5810279.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


pgsql-general by date:

Previous
From: Arup Rakshit
Date:
Subject: Re: Not able to understand how to write group by
Next
From: Craig Ringer
Date:
Subject: Windows releases - Bundle OpenSSL includes and .libs in the installer?