0

Trying to do a query but need to add 250 to TotalCost if mileage is > 150.

select milegae.dc_id, 
mileage.store_id, 
mileage.mileage, 
round((mileage.mileage * .75 + 200),2) as TripCost
from mileage;

This query gives me the data I want but I'm unsure how to add 250 to the TotalCost given the parameters.

0

2 Answers 2

1

You can do it with CASE EXPRESSION :

select milegae.dc_id, 
    mileage.store_id, 
    mileage.mileage, 
    CASE WHEN mileage.mileage > 150 
         THEN ROUND(((mileage.mileage+250) * .75 + 200),2) 
         ELSE ROUND((mileage.mileage * .75 + 200),2) 
    END as TripCost
from mileage;
Sign up to request clarification or add additional context in comments.

2 Comments

dude, I think 250 should add after all calculations.
Maybe, not clear at all... The OP can do some of the work too :) @Ullas
0

Use a CASE expression to check whether the mileage > 150.

Hope you want to add 250 to the final value if the mileage value is greater than 150.

Query

SELECT 
  milegae.dc_id, 
  mileage.store_id, 
  mileage.mileage,
  CASE WHEN mileage.mileage > 150 
    THEN round((mileage.mileage * .75 + 200),2) + 250
    ELSE round((mileage.mileage * .75 + 200),2) 
  END AS TripCost
FROM mileage;

1 Comment

Thank you!!! I tried that but my Syntax was incorrect. Your solution works perfectly!!!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.