Thread

  1. Re: [SQL] Re: [HACKERS] Counting bool flags in a complex query

    Herouth Maoz <herouth@oumail.openu.ac.il> — 1999-07-19T12:11:26Z

    At 11:37 +0300 on 16/07/1999, Michael Richards wrote:
    
    
    > My folder numbers are: negative numbers are system folders such as New
    > mail, trash, drafts and sentmail. I wanted to order the tuples so that the
    > folderids were sorted from -1 to -4, then 1 to x. This way the system
    > folders would always appear first in the list.
    >
    > This may not be valid SQL, as none of my books mention it. Is it possible
    > to order by an expression?
    >
    > Here are some examples which some some odd behaviour. My suspected bug
    > findings are at the end:
    
    I think the problem results from using non-standard constructs such as
    order by expression, and indeed ordering by columns that don't appear in
    the select list.
    
    If you want to do the best by yourself, put the expression by which you
    order in the select list. A simple example would be:
    
    Instead of:
      SELECT f1, min( f2 ), max ( f3 )
      GROUP BY f1
      ORDER BY expr( f1 );
    
    Use:
    
      SELECT expr( f1 ) AS ordcol, f1, min( f2 ), max( f3 )
      GROUP BY ordcol, f1
      ORDER BY ordcol;
    
    What is the difference? The difference is that now GROUP BY (which also
    does internal sorting) knows about that expression and considers it. Since
    ordcol is the same for each value of f1, this should not change the groups.
    This simply makes sure all parts of the query are aware of what is being
    done around them. This is also the standard, as far as I recall.
    
    What's the problem? You have a column in the output that you didn't really
    want. But hey, why should that bother you? If you're reading it through
    some frontend, simply have it ignore the first column that returns.
    
    Herouth
    
    --
    Herouth Maoz, Internet developer.
    Open University of Israel - Telem project
    http://telem.openu.ac.il/~herutma