0

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

2 Answers 2

2

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
2
  • Can i initialize sc to zero ?? (The value of service_charge)
    – Pawan
    Nov 20, 2014 at 21:10
  • @PreethiJain of course you can SELECT @sc := 0 AS service_charge, but there's no need to initialize @sc to zero, it will be calculated for every row. You just have to make sure that you will use it after it has been set.
    – fthiella
    Nov 20, 2014 at 21:14
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.