pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joseph Shraibman <jks@selectacast.net>
To: pgsql-sql@postgresql.org
Subject: sorting by day of the week
Date: Tue, 24 Jan 2006 20:23:50 -0500
Message-ID: <dr6jv0$emi$2@news.hub.org> (raw)

p8:owl=>SELECT to_char( logtime, 'Dy'),count(*) FROM logtab WHERE 
date_trunc('day', logtime) > current_date + '7 day ago'::interval group 
by to_char( logtime, 'Dy') ORDER BY to_char( logtime, 'Dy') DESC;
  to_char | count
---------+-------
  Wed     |  1447
  Tue     |   618
  Thu     |  1161
  Sun     |   230
  Sat     |   362
  Mon     |   760
  Fri     |  1281
(7 rows)

The problem is that I want those results sorted in day of week order, 
not text order of the day name, so I tried this:

p8:owl=>SELECT to_char( logtime, 'Dy'),count(*) FROM sclog WHERE 
date_trunc('day', logtime) > current_date + '7 day ago'::interval group 
by to_char( logtime, 'Dy') ORDER BY to_char( logtime, 'D') DESC;
ERROR:  column "sclog.logtime" must appear in the GROUP BY clause or be 
used in an aggregate function

Now obviously I don't want to group by logtime (a timestamp) so how do I 
work around this?  What I really need is a function that converts from 
the char representation to a day of week number or vice versa.  I also 
have the same problem with month names.



view thread (3+ messages)  latest in thread

Message-ID: <dr6jv0$emi$2@news.hub.org>
Permalink:  ../dr6jv0$emi$2@news.hub.org/
Also on:    postgresql.org/message-id/dr6jv0$emi$2@news.hub.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jks@selectacast.net
  Subject: Re: sorting by day of the week
  In-Reply-To: <dr6jv0$emi$2@news.hub.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox