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

From: Andreas Kretschmer <akretschmer(at)spamfence(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: adding "order by" to a "group by" query
Date: 2008-12-06 18:23:25
Message-ID: 20081206182324.GA8966@tux
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com> schrieb:
> >> You can use a subquery like my example:
> >>
> >> test=*# select i, comma(t) from (select distinct i,t from foo) bar group by i;
> >> i | comma
> >> ---+---------
> >> 1 | a, b, c
> >> (1 row)
> >>
> >> Time: 0.554 ms
> >> test=*# select i, comma(t) from (select distinct i,t from foo order by t desc) bar group by i;
> >
> > Thanks Andreas, that would be good solution.
> >
> > (still curious about the "must be used in an aggregate function" error
> > though... because I do use it in an aggregate)
>
> You can order by the same thing you selected:
>
> select sum(i) from ... group by j order by sum(i)
> OR
> select sum(i) from ... group by j order by 1 -- 1 stands for the
> first select list item...

Not in this case:

test=# select i, comma(distinct t ) from foo group by i order by
comma(t) desc;
i | comma
---+---------
1 | a, b, c
(1 row)

doesn't work as desired.

Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly." (unknown)
Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message John Lister 2008-12-06 18:24:25 Re: adding "order by" to a "group by" query
Previous Message Scott Marlowe 2008-12-06 17:40:43 Re: adding "order by" to a "group by" query