Gene,

I've found the very same inconsistency in VFP, MySQL, and three different MS
SQL Server versions.  

I'd guess that all of these SQL systems total columns not one column at a
time (in which case your first query should work) but by one row at a time.
If that is indeed the case, then sumqty isn't defined until all rows have
been processed.  When processing is complete, though, then avgunit exists,
and the output can be sorted by it.

HTH,
   Mike


-----Original Message-----
From: ProfoxTech [mailto:[email protected]] On Behalf Of Gene
Wirchenko
Sent: Wednesday, November 11, 2015 12:29 PM
To: [email protected]
Subject: SQL: Renaming Columns Problem

Dear Vixens and Reynards:

      An innocent-looking query:
           select;
            clcode,;
            sum(basicchg+ovrhdfee) as totamount,;
            sum(quantity) as sumqty,;
            totamount/sumqty as avgunit;
           from cwkt;
           group by clcode;
           order by clcode
Unfortunately, it is not legal.  Instead, I have to do:
           select;
            clcode,;
            sum(basicchg+ovrhdfee) as totamount,;
            sum(quantity) as sumqty,;
            sum(basicchg+ovrhdfee)/sum(quantity) as avgunit;
           from cwkt;
           group by clcode;
           order by clcode
(The change is in the definition of avgunit.)

      Apparently, I can not use the totamount and sumqty names in the
columns clause.  Oddly enough, I can use them in the order by clause (and
maybe elsewhere).  This is legal:
           select;
            clcode,;
            sum(basicchg+ovrhdfee) as totamount,;
            sum(quantity) as sumqty,;
            sum(basicchg+ovrhdfee)/sum(quantity) as avgunit;
           from cwkt;
           group by clcode;
           order by avgunit

      Is this really how it is, or am I overlooking something?

Sincerely,

Gene Wirchenko


[excessive quoting removed by server]

_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://mail.leafe.com/mailman/listinfo/profox
OT-free version of this list: http://mail.leafe.com/mailman/listinfo/profoxtech
Searchable Archive: http://leafe.com/archives/search/profox
This message: 
http://leafe.com/archives/byMID/profox/[email protected]
** All postings, unless explicitly stated otherwise, are the opinions of the 
author, and do not constitute legal or medical advice. This statement is added 
to the messages for those lawyers who are too stupid to see the obvious.

Reply via email to