I am wondering if it is possible to innner join on the greates value.
So for example
SELECT *
FROM Vehicle v
INNER JOIN VehicleCost vc ON v.VehicleWeight > vc.WeightFrom AND c.VehicleWeight < vc.WeightTo
But if v.VehicleWeightTotal > v.VehicleWeight I want that to replace v.VehicleWeight on the inner join. So potentially this could be a different join for each vehicle.
How would I go about doing that check within the join?
Sample:
tblVehicle
VehicleId VehicleWeight VehicleWeightTotal
1 12 15
2 1 8
3 16 20
tblVehicleCost
WeightFrom WeightTo Dollars
0 5 1
6 11 8
12 16 9
17 20 15
So:
Vehicle 1 = 9
Vehicle 2 = 8
Vehicle 3 = 15
Your requirements can be more simply expressed as:
This is how you express that in SQL:
I took the liberty of changing your range check to use
between, but if it’s not OK to match on the value equalling the range limits – ie using>=and<=in your query – then just repeat thecasestatement for each side of the range.