Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 606
  • Last Modified:

reusing calculated column (function)

I am trying to reuse a calculated field in mysql, the calculated field is a stored function. I am not able to reuse the output of the calculate_age function (age) as an input for the expected_income function as i get error "1054" unknown column "age" in the 'field list'.

Any Ideas?
 
select name, calculate_age(dob) as age, expected_income(age) as income from tbl;

Open in new window

0
phpmysqlcoder
Asked:
phpmysqlcoder
  • 5
  • 2
1 Solution
 
_agx_Commented:
Not able how? If you're getting an error can you post the error message?
0
 
phpmysqlcoderAuthor Commented:
I get error "1054" unknown column "age" in the 'field list'.
0
 
_agx_Commented:
If you wanted to use the results of another function, I think you'd either have to nest them

          SELECT calculate_age(dob) as age, expected_income( calculate_age(dob) ), .....

... which would mean running the function twice OR use a derived table

        SELECT  expected_income( t.age ) ....
        FROM    ( SELECT  name, calculate_age(dob)  as age, .... FROM tableName )  t

0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
_agx_Commented:
Out of curiosity what do these functions do? Is it something that can't be achieved with straight JOINs?
0
 
phpmysqlcoderAuthor Commented:
Actually I am just simplifying the problem. The actual 1st function has many arguments and I am looking for a way to have the calculated results from the 1st function as an input in the 2nd function.
0
 
_agx_Commented:
Did you try the two options I mentioned above? ie Either nest the functions OR use a derived query so the result of function 1 is available as an actual column? Aside from reworking the functions, those are the only 2 methods I know of.

0
 
_agx_Commented:
ie

obviously the calculated value doesn't exist yet when running function2, because they're executed in parallel.  afaik your only choices are to either feed the results of function1 into function2

ie nesting...
select name, calculate_age(dob) as age, expected_income( calculate_age(dob) ) as income from tbl;

... OR used a derived table so that calculated value *does* exist as a column

derived table
select t.name, t.age, expected_income(t.age) as income
from  (select name, calculate_age(dob) as age from tbl ) t


0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now