Get multiple columns with counts from one table. - Mailing list pgsql-general

From chuydb
Subject Get multiple columns with counts from one table.
Date
Msg-id 1371074953668-5758977.post@n5.nabble.com
Whole thread Raw
Responses Re: Get multiple columns with counts from one table.  (Alban Hertroys <haramrae@gmail.com>)
Re: Get multiple columns with counts from one table.  (Sergey Konoplev <gray.ru@gmail.com>)
List pgsql-general
Hi,
From two columns in my table I want to get a unified count for the values in
these columns.
As an example, two columns are:

Table: reports
|    type        |    place        |
-----------------------------------------
|    one        |    home        |
|    two        |    school    |
|    three        |    work        |
|    four        |    cafe        |
|    five        |    friends    |
|    six         |    mall        |
|    one        |    work        |
|    one        |    work        |
|    three        |    work        |
|    two        |    cafe        |
|    five        |    cafe        |
|    one         |    home        |

If I do:
SELECT type, count(*) from reports
group by type

I get:
|    type        |    count    |
----------------------------------
|    one        |    4    |
|    two        |    2    |
|    three        |    2    |
|    four        |    1    |
|    five        |    2    |
|    six         |    1    |

Im trying to get something like this: (one column with my types grouped
together and multiple columns with the count vales for each place)
I get:
|    type        |    home    |    school    |    work    |    cafe    |    friends    |    mall    |
-------------------------------------------------------------------------------------------------------------------
|    one        |    2    |            |    2    |        |            |        |
|    two        |        |    1        |        |    1    |            |        |
|    three        |        |            |    2    |        |            |        |
|    four        |        |            |        |    1    |            |        |
|    five        |        |            |        |    1    |    1        |        |
|    six         |        |            |        |        |            |    1    |

which would be the result of running a count for every place like this:
SELECT type, count(*) from reports where place  = 'home'
group by type
SELECT type, count(*) from reports where place  = 'school'
group by type
SELECT type, count(*) from reports where place  = 'work'
group by type
SELECT type, count(*) from reports where place  = 'cafe'
group by type
SELECT type, count(*) from reports where place  = 'friends'
group by type
SELECT type, count(*) from reports where place  = 'mall'
group by type

Is this possible with postgresql???

Thanks in advance.




--
View this message in context:
http://postgresql.1045698.n5.nabble.com/Get-multiple-columns-with-counts-from-one-table-tp5758977.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Pass-by-reference UDTs and volatility
Next
From: Alban Hertroys
Date:
Subject: Re: Get multiple columns with counts from one table.