Re: Grouping by date range

From: Ron Johnson <ron(dot)l(dot)johnson(at)cox(dot)net>
To: PgSQL General ML <pgsql-general(at)postgresql(dot)org>
Subject: Re: Grouping by date range
Date: 2003-08-20 18:44:59
Message-ID: 1061405098.29044.6.camel@haggis
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, 2003-08-20 at 12:47, Tom Lane wrote:
> Ron Johnson <ron(dot)l(dot)johnson(at)cox(dot)net> writes:
> > On Tue, 2003-08-19 at 02:56, Alexander Litvinov wrote:
> >> select date_part('year', date), count(*) from your_table group by
> >> date_part('year', date) order by date_part('year', date);
>
> > Is the ORDER BY really needed here?
>
> If you want the results ordered that way, yes.

Hmmmmm. I don't think so, if the ORDER BY clause is exactly the
same as the GROUP BY clause, which is the case here:
select date_part('year', date), count(*)
from your_table
group by date_part('year', date)
order by date_part('year', date);

The GROUP BY does implicit sorting, so an ORDER BY on the exact same
column(s) as the GROUP BY is redundant.

--
-----------------------------------------------------------------
Ron Johnson, Jr. ron(dot)l(dot)johnson(at)cox(dot)net
Jefferson, LA USA

"Adventure is a sign of incompetence"
Stephanson, great polar explorer

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jan Wieck 2003-08-20 18:45:23 Re: Buglist
Previous Message The Hermit Hacker 2003-08-20 18:41:15 Re: Mailing list in French