List:General Discussion« Previous MessageNext Message »
From:Michael Dykman Date:April 8 2011 9:12pm
Subject:Re: Ordering by grouping
View as plain text  
Mildly surprised this is working at all.  Your name alias fields like
"When" should be quoted by back-tics, no double quotes..   this is a
clarification and does not solve the issue really although it might
make your final result less surprising.

A key thing to note about GROUP BY is that, although it uses ordering
internally to do it's work, it is not an ORDER BY clause and does not
pretend to make guarantees as to the order of the resulting data.  If
you need ordering, add an ORDER BY clause after your GROUP BY.

 - michael dykman

On Fri, Apr 8, 2011 at 8:51 AM,  <hsv@stripped> wrote:
> Once more I am surprised by the ordering that I get from 'GROUP BY'.
>
> This defines the table of directors that have been on the board:
>
> CREATE TABLE DIRECTOR
> (       Chosen  DATE NOT NULL
> ,       Through DATE NOT NULL
> ,       MemberID        INTEGER REFERENCES
> MemberAddress (MemberID)
> ,       CONSTRAINT dpk PRIMARY KEY (Chosen, MemberID)
> ,       Rank    TINYINT REFERENCES MemberName (Rank)
> )
>
> This query, based also on a view ('offboard') that joins this table with
> name&address tables, lists the boards that arise from the table:
>
> select "When", COUNT(givenname || ' ' || surname) AS directors,
> group_concat(givenname || ' ' || surname ORDER BY Surname) AS Board
> FROM (select distinct chosen AS "When"
>        FROM director
>        UNION select distinct ADDDATE(through, 1)
>        FROM director
>        WHERE through < CURDATE()) as B JOIN offboard ON "When"
> between chosen and through
> GROUP by "When"
>
> It is only roughly, not completely, ordered by '"When"'. Why? When is 'GROUP-BY'
> ordering complete?
>
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:    http://lists.mysql.com/mysql?unsub=1
>
>



-- 
 - michael dykman
 - mdykman@stripped

 May the Source be with you.
Thread
Ordering by groupinghsv8 Apr
  • Re: Ordering by groupingMichael Dykman8 Apr
    • Re: Ordering by groupinghsv10 Apr