Alex Rivera | Logout

Adding MySQL alias fields together

Asked 2010-07-30T16:50:17.870
26

Consider a query similar to:

 SELECT sum(EXPR) as total,
        sum(EXPR) as total2, 
        sum(total+total2) as grandtotal 
 FROM tablename

This comes up and says unknown column total in field list.

Is there anyway to reference the alias fields in a calculation without retyping the sum expression because sum(EXPR) on each side is very long.

Edit
Report

2 Answers

0
SELECT
WITH Sums AS
(
 SELECT sum(EXPR) as total, 
        sum(EXPR) as total2
 FROM tablename 
)
SELECT SUM(sums);

for you tsql fans...

answered 2010-07-30T16:56:41.270
0
answered 2010-07-30T16:56:54.550

Your Answer