Re: Formatting a month in query output - Mailing list pgsql-novice

From Alexander Borkowski
Subject Re: Formatting a month in query output
Date
Msg-id 41F82962.8070908@abri.une.edu.au
Whole thread Raw
In response to Re: Formatting a month in query output  ("Rodolfo J. Paiz" <rpaiz@simpaticus.com>)
Responses Re: Formatting a month in query output
List 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

pgsql-novice by date:

Previous
From: Tom Lane
Date:
Subject: Re: SQL and function reference?
Next
From: "Rodolfo J. Paiz"
Date:
Subject: Re: Two copies of every mail!