Re: Get multiple columns with counts from one table.

From: Sergey Konoplev <gray(dot)ru(at)gmail(dot)com>
To: chuydb <jdelbosque(at)cic(dot)mx>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Get multiple columns with counts from one table.
Date: 2013-06-12 22:55:12
Message-ID: CAL_0b1sb6kfNgM5xGSTR_RBdw_0yiOQax19a3ieGUaKwgJqqKQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Jun 12, 2013 at 3:09 PM, chuydb <jdelbosque(at)cic(dot)mx> wrote:
> 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 |

First you need to get the counts by grouping by type and place
together, and then crosstab(text source_sql, text category_sql) from
the tablefunc module will help you to get this.

http://www.postgresql.org/docs/9.2/static/tablefunc.html#AEN144882

It is documented pretty good and has a lot of useful examples.

--
Kind regards,
Sergey Konoplev
PostgreSQL Consultant and DBA

Profile: http://www.linkedin.com/in/grayhemp
Phone: USA +1 (415) 867-9984, Russia +7 (901) 903-0499, +7 (988) 888-1979
Skype: gray-hemp
Jabber: gray(dot)ru(at)gmail(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message chuydb 2013-06-12 23:14:33 Re: Get multiple columns with counts from one table.
Previous Message Alban Hertroys 2013-06-12 22:21:53 Re: Get multiple columns with counts from one table.