Re: Formatting a month in query output

From: Alexander Borkowski <alexander(dot)borkowski(at)abri(dot)une(dot)edu(dot)au>
To: "Rodolfo J(dot) Paiz" <rpaiz(at)simpaticus(dot)com>
Cc: Sean Davis <sdavis2(at)mail(dot)nih(dot)gov>, pgsqlnovice <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Formatting a month in query output
Date: 2005-01-26 23:36:02
Message-ID: 41F82962.8070908@abri.une.edu.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

> flightlog=> select to_char(date,'Mon YYYY') as month, count(num) as num,
> sum(hrs_total) as hours from flights group by month order by date asc;
>
> ERROR: column "flights.date" must appear in the GROUP BY clause or be
> used in an aggregate function

Try this:

select to_char(date,'Mon YYYY') as month, count(num) as num,
sum(hrs_total) as hours from flights group by date, month order by date asc;

> Using "order by date"
> gets me an error since the date column is not used in the query.

It is (for sorting). "date" is just not grouped and you can't do this,
which is what the above error message is all about: you have group it or
use an aggregate function (e.g. count, sum, ...) on it as you do for all
other columns in that query.

Cheers,

Alex

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Rodolfo J. Paiz 2005-01-27 00:40:32 Re: Two copies of every mail!
Previous Message Tom Lane 2005-01-26 23:34:49 Re: SQL and function reference?