Skip site navigation (1) Skip section navigation (2)

Re: [HACKERS] No: implied sort with group by

From: darrenk(at)insightdist(dot)com (Darren King)
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: [HACKERS] No: implied sort with group by
Date: 1998-01-27 13:43:29
Message-ID: 9801271343.AA65898@ceodev (view raw, whole thread or download thread mbox)
Lists: pgsql-hackers
> > Does the SQL standard say anything about an implied sort when
> > grouping or is it up to the user to include an ORDER BY clause?
> > darrenk
> Without order by the result set is never guaranteed to be ordered in a
> specific way (standard speak). The order is dependent on the chosen
> query path, which changes from query to query.
> Andreas

Postgres should then do an internal sort before grouping.  In the second
of your examples, I take the above to mean that either row could be
returned first.

In order to get that result set though, the data needs to be sorted before
getting to the group by node in the executor.  The order of that internal
sort is purely arbitrary, it just has to be done.

This is what I think is missing or broken right now.

> > select * from t1;
>          a b  c
>          1    x
>          2    x
>          3    z
>          2    x
> 4 row(s) retrieved.
> > select b,c,sum(a) from t1 group by b,c;
> b  c             (sum)
>    x                 5
>    z                 3
> 2 row(s) retrieved.



pgsql-hackers by date

Next:From: SerjDate: 1998-01-27 14:03:41
Subject: Group By bug in snapshot 270198
Previous:From: Bruce MomjianDate: 1998-01-27 12:37:07
Subject: Re: [HACKERS] Snapshot 270198 compile error

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group