Bug #42667 Feature: Adding derived/aggregate columns together using aliases
Submitted: 6 Feb 2009 22:45 Modified: 23 Mar 2009 15:52
Reporter: Chloe S Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: DML Severity:S4 (Feature request)
Version:5.0.60 OS:Linux
Assigned to: CPU Architecture:Any
Tags: addition, sum, syntactic sugar

[6 Feb 2009 22:45] Chloe S
Description:
I would like to be able to do this:
mysql> select sum(a) as xa, sum(b) as xb, xa+xb as total from (select 1 as a, 2 as b union select 3,4) t;
ERROR 1054 (42S22): Unknown column 'xa' in 'field list'

Instead I have to do this:
mysql> select sum(a) as xa, sum(b) as xb, sum(a)+sum(b) as total from (select 1 as a, 2 as b union select 3,4) t;
+------+------+-------+
| xa   | xb   | total |
+------+------+-------+
|    4 |    6 |    10 |
+------+------+-------+
1 row in set (0.01 sec)

How to repeat:
mysql> select 1 as a, 2 as b, a+b as total;
ERROR 1054 (42S22): Unknown column 'a' in 'field list'
[23 Mar 2009 15:52] Susanne Ebrecht
Many thanks for writing a feature request.

The actual behaviour is like SQL standard wants it.

I will let the decision to our development if they want to implement it or not.