Why do it all in the SQL statment? Why not
Select Sum(customer_costs.totalpr
Then use the same values for all your calculations.
$Profit = $customercosts - $actualcosts;
$Margin = $Profit / $customercosts;
Main Topics
Browse All TopicsHello all!
I am having issues using the alias for a SUM later in my SQL SELECT statement.
What I am trying to get to work is:
SELECT (SELECT SUM(total_price) FROM customer_costs WHERE job_number = '46') - (SELECT SUM(total_price) FROM actual_costs WHERE job_number = '46') as profit, SUM(profit / (SELECT SUM(total_price) FROM customer_costs WHERE job_number = '46') as margin
I currently receive an error from MySQL saying it doesn't know what table profit comes from. As you can see, it is calculated above.
Also, would it be possible for me to alias (SELECT SUM(total_price) FROM customer_costs WHERE job_number = '46') as cust_total and use it in the division with profit to make the margin (so I don't have to repeat what's already written in the first subquery)?
Is there some way to do what I am attempting to do without restating the same SQL to recreate the value for profit again? Thanks for your help!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Why do it all in the SQL statment? Why not
Select Sum(customer_costs.totalpr
Then use the same values for all your calculations.
$Profit = $customercosts - $actualcosts;
$Margin = $Profit / $customercosts;
Business Accounts
Answer for Membership
by: angelIIIPosted on 2007-04-20 at 08:51:42ID: 18947351
yes, but mysql cannot directly use column aliases.
honestly, I would NOT try to solve that in (my)SQL directly, but in front-end code ...
in MSSQL or ORACLE, this would be much easier, but MySQL is still a bit new for such kind of syntax