Re: counts of groupings by date year-month

From: Andreas Kretschmer <akretschmer(at)spamfence(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: counts of groupings by date year-month
Date: 2009-02-27 21:52:16
Message-ID: 20090227215215.GA12288@tux
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Carol Cheung <cacheung(at)consumercontact(dot)com> wrote:

> Hi,
> I have a table called temp
>
> access_date | active | status
> -------------+--------+--------
> 2009-02-01 | t | 15
> 2009-02-01 | f | 16
> 2009-02-02 | f | 17
> 2009-02-01 | t | 17
> 2009-02-02 | f | 21
> 2009-01-01 | t | 20
> 2009-01-01 | t | 21
> 2009-01-01 | f | 21
>
>
> What I want is to be able to get counts of active by year-month. So the
> output would be like:
>
> year_month | count
> ------------+-------
> 200901 | 3
> 200902 | 5
>
> I tried something like
> SELECT to_char(access_date, 'YYYYMM') as year_month, count(year_month)
> FROM temp GROUP BY year_month ORDER BY year_month;
>
> but I'm unable to execute this query because the column "year_month"
> doesn't exist in temp table.
>
> Is it possible to get counts by year_month?

change count(year_month) to count(1), untested.

Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly." (unknown)
Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Emi Lu 2009-03-03 20:26:22 "union" vs. left join
Previous Message Tom Lane 2009-02-27 21:50:09 Re: counts of groupings by date year-month