Re: extract (dow/week from date)

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: Clodoaldo Pinto <clodoaldo(dot)pinto(at)gmail(dot)com>, "pgsql-general postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: extract (dow/week from date)
Date: 2005-08-21 01:54:55
Message-ID: 20050820184003.P30120@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Sat, 20 Aug 2005, Bruce Momjian wrote:

> Uh, you are ordering by 'date', not column 3, try ORDER BY 3.

That's not really the issue. The issue is that our definition of date of
week and week of year are somewhat inconsistent with each other. We appear
to be doing week of year per ISO-8601, but what the descriptions I've seen
of that use days 1-7 for Monday-Sunday, whereas we're apparently giving
0-6 for Sunday-Saturday. This means that sorting by (week of year, day of
week) will sort Sundays oddly (since it would for example below sort the
14th before the 8th).

> ---------------------------------------------------------------------------
>
> Clodoaldo Pinto wrote:
> > The extract (dow from date) function returns 0 for Sunday (nice).
> >
> > My problem is that Sunday is the last day of the week according to
> > extract (week from date). Is it the expected behavior?
> >
> > teste=# create table dates (date timestamp);
> > CREATE TABLE
> > teste=# insert into dates values ('2005-08-08');
> > INSERT 0 1
> > teste=# insert into dates values ('2005-08-09');
> > INSERT 0 1
> > teste=# insert into dates values ('2005-08-10');
> > INSERT 0 1
> > teste=# insert into dates values ('2005-08-11');
> > INSERT 0 1
> > teste=# insert into dates values ('2005-08-12');
> > INSERT 0 1
> > teste=# insert into dates values ('2005-08-13');
> > INSERT 0 1
> > teste=# insert into dates values ('2005-08-14');
> > INSERT 0 1
> > teste=# select date, extract (week from date) as week, extract (dow
> > from date) as dow
> > teste-# from dates
> > teste-# order by date;
> > date | week | dow
> > ---------------------+------+-----
> > 2005-08-08 00:00:00 | 32 | 1
> > 2005-08-09 00:00:00 | 32 | 2
> > 2005-08-10 00:00:00 | 32 | 3
> > 2005-08-11 00:00:00 | 32 | 4
> > 2005-08-12 00:00:00 | 32 | 5
> > 2005-08-13 00:00:00 | 32 | 6
> > 2005-08-14 00:00:00 | 32 | 0
> > (7 rows)
> >
> > In mysql the date functions work as I need it:
> > order by yearweek(day, 2) desc, dayofweek(day);

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2005-08-21 02:06:39 Re: extract (dow/week from date)
Previous Message Alvaro Herrera 2005-08-21 01:51:36 Re: extract (dow/week from date)