Re: Grouping by date range

From: Mike Mascari <mascarm(at)mascari(dot)com>
To: Mat <psql-mail(at)freeuk(dot)com>
Cc: pgsql general list <pgsql-general(at)postgresql(dot)org>
Subject: Re: Grouping by date range
Date: 2003-08-19 07:50:07
Message-ID: 3F41D6AF.3020406@mascari.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Mat wrote:
> Hi,

> I have a table with two column, date and data
> I would like to do a set of queries to generate statistics on the data,
> such as count(data) for month blocks and year blocks. What is the best
> way to accomplish this?
>
>
> dd/mm/yy
> date | data
> ---------------
> 01/01/01| 123
> 01/01/01| abc
> 02/01/01| def
> 03/03/01| hij
>
> SOME QUERY ....
>
> Year | Count
> -------------
> 01 | 3
>
> I can see how to group by day - but how do i go about decreasing the
> precision down to months/years.

SELECT COUNT(*)
FROM mytable
GROUP BY date_trunc('month', date);

See:

http://www.postgresql.org/docs/7.3/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC

for details.

Hope that helps,

Mike Mascari
mascarm(at)mascari(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alexander Litvinov 2003-08-19 07:56:45 Re: Grouping by date range
Previous Message Shridhar Daithankar 2003-08-19 07:47:07 Re: Example Database