Re: Problem with the group statement...

From: Paul G. Brown <paul_geoffrey_brown_at_yahoo.com>
Date: 18 Jul 2002 14:14:51 -0700
Message-ID: <57da7b56.0207181314.5d083ccc_at_posting.google.com>


GoranG <no_at_spam.net> wrote in message news:<cbhdju08qdliltg95l5oqisqu4vo7tbhks_at_4ax.com>...
> On Thu, 18 Jul 2002 13:12:39 +0200, "Volker Hetzer"
> <volker.hetzer_at_ieee.org> wrote:
>
> >
> >"Lennart Jonsson" <lennart_at_kommunicera.umea.se> wrote in message news:6dae7e65.0207170906.5d1c6712_at_posting.google.com...
> >> [Good explanation of grouping]
> >> the order by is applied last
> >So the result of the group clause is sorted.
> >
> >Lots of thanks!
> >Volker
>
> Hhmm, not sure about that.

   You're quite right not to be sure.

   Sorting input records is only one way to implement group by. Hashing is   another approach that is actually preferred because it is generally   more efficient (except in a couple of cases that can generally be inferred   from the query and the DBMS's statistics).  

   If your DBMS uses a hash-based approach to GROUP BY then the results will be   in no particular order.

   If you want ordering, you need to be explicit about it in your SQL.

   SELECT C.Cust_Name, SUM(C.Order)
     FROM Customers C
   GROUP BY C.Cust_Name ORDER BY C.Cust_Name;

   KR

             Pb Received on Thu Jul 18 2002 - 23:14:51 CEST

Original text of this message