Search Postgresql Archives

Re: Not able to understand how to write group by

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux