Re: adding "order by" to a "group by" query

From: Louis-David Mitterrand <vindex+lists-pgsql-sql(at)apartia(dot)org>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: adding "order by" to a "group by" query
Date: 2008-12-06 18:36:20
Message-ID: 20081206183620.GA25679@apartia.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Sat, Dec 06, 2008 at 06:24:25PM +0000, John Lister wrote:
> >(still curious about the "must be used in an aggregate function" error
> >though... because I do use it in an aggregate)
>
> You're original query grouped on the person id and name, therefore you
> can only return (and order by) these functions or the result of an
> aggregate function on other columns (such as the array_accum function).
>
> I'm no expert, but I think the error is slightly misleading, normally
> you would order by the result of an aggregate function but maybe the
> parser does this implicitly for you sometimes. does
>
> select p.id_person, person_name(p), array_accum(distinct pt.type_fr)
> from person p
> left join person_to_event x using (id_person)
> left join person_type pt using (id_person_type)
> where person_name(p) ilike '%will%' group by
> p.id_person,person_name(p)
> order by 3;
>
> work for you?

Not quite. But thanks for your suggestion John: I just learned that one
can supply an index to an order clause.

Actually what I'd like to be able to do is: put the (say) 'actors' in
front of the list. The catch is that a person can have several
person_type's (through the person_to_event table: id_person, id_event,
id_person_type).

--
http://www.critikart.net

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Azzeddine Daddah 2008-12-06 22:22:58 Public synonyms
Previous Message John Lister 2008-12-06 18:24:25 Re: adding "order by" to a "group by" query