sql - MySQL: Using alias in select arithmetic - Unknown field -


here's select:

select    sum(t.amount + c.designfeevalue) cashreceived,    round(sum(i.value) * (m.percentours / 100)) advalue,   m.managementfee managementfee,   m.productioncost productioncost,   5 emailaddress,   (     (       select value       commission_transactions       isdebit       true     ) -     (       select value       commission_transactions       isdebit       false     )   ) miscexpenses,   (managementfee + productioncost + emailaddress + miscexpenses) totalexpenses 

this bombing because of following line, add aliases.

(managementfee + productioncost + emailaddress + miscexpenses) totalexpenses 

the aliases unknown fields.

is there way can keep aliases arithmetic or need re-do math generates each alias calculation of totalexpenses? seems ugly way it.


update:

per suggestions, using derived table.

select    cashreceived,   advalue,   managementfee,   productioncost,   emailaddress,   miscexpenses,   advalue + managementfee + productioncost + emailaddress + miscexpenses totalexpenses (   select     sum(t.amount + c.designfeevalue) cashreceived,     round(sum(i.value) * (m.percentours / 100)) advalue,     m.managementfee managementfee,     m.productioncost productioncost,     5 emailaddress,     (       (         select value         commission_transactions         isdebit true       ) -       (         select value         commission_transactions         isdebit false       )     ) miscexpenses     magazines m   join insertions on i.magazineid = m.id   join transactions t on t.insertionid = i.id   join contracts c on i.contractid = c.id   join commission_transactions ct on m.id = ct.magazineid   m.id = 17     , t.ischargedback false     , t.`timestamp` >= '2013-08-01 00:00:00'     , t.`timestamp` < '2013-09-01 00:00:00'     , ct.createddate >= '2013-08-01 00:00:00'     , ct.createddate < '2013-09-01 00:00:00' ) sub; 

aliases aren't available re-use within field list, e.g:

mysql> select 5 five, 5 + 1 six; error 1054 (42s22): unknown column 'five' in 'field list' 

you'll have wrap select one, alias math in wrapper, e.g.

select *, managementfee + productioncost + emailaddress + miscexpenses) totalexpenses (    ... above query here ... ) 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -