Pawan
Pawan

Reputation: 32321

How to use the Computed Value to calculate another

I am using mysql

I have a query as shown below

Select 
  cost,
  (service_charge*(cost-Discount)) as service_charge,
  (service_tax*(cost-Discount+(service_charge*(cost-Discount)))) as service_tax
From VENDOR_ITEMS
WHERE vendor_items_id = 264

My question is that the value i get as service_charge can i use that in the calculation of service_tax ??

Please let me know if this is possible

Upvotes: 0

Views: 38

Answers (2)

fthiella
fthiella

Reputation: 49049

You cannot use a computed value to calculate another, but you can use variables:

SELECT
  cost,
  @sc := (service_charge*(cost-Discount)) as service_charge,
  (service_tax*(cost-Discount+( @sc ))) as service_tax
FROM
  VENDOR_ITEMS
WHERE
  vendor_items_id = 264

Upvotes: 2

Mihai
Mihai

Reputation: 26784

You have to repeat the expression

Select cost ,
 (service_charge*(cost-Discount)) as service_charge ,
 (service_tax*(cost-Discount+((service_charge*(cost-Discount))*(cost-Discount)))) as service_tax 
From VENDOR_ITEMS WHERE vendor_items_id = 264

Upvotes: 0

Related Questions