Select 

SUM(CASE 
WHEN (cp.start <= 10 AND cp.end >= 10) THEN cp.price * crh.value * (10 - cp.start + 1) 
WHEN (cp.start <= 10 AND cp.end <= 10) THEN cp.price * crh.value * (cp.end - cp.start + 1) 
ELSE 0 END) AS TOTAL_PRICE, 

(SELECT cpf.price * crhf.value from vehicle_class_distance_price cpf 
LEFT JOIN currency_rate as crf ON from_currency_id = cpf.currency_id AND to_currency_id = 141 
LEFT JOIN currency_rate_history as crhf ON rate_id = crf.id AND crhf.id = (SELECT id FROM currency_rate_history WHERE rate_id = crf.id ORDER BY id DESC LIMIT 1 ) 
WHERE cpf.vehicle_class_id=1 AND cpf.company_id=2 AND cpf.start >= 0 LIMIT 1) as FIRST_PRICE 
FROM vehicle_class_distance_price cp 

LEFT JOIN currency_rate as cr ON from_currency_id = cp.currency_id AND to_currency_id = 141 
LEFT JOIN currency_rate_history as crh ON crh.rate_id = cr.id AND crh.id = (SELECT id FROM currency_rate_history WHERE rate_id = cr.id ORDER BY id DESC LIMIT 1 ) 
WHERE cp.vehicle_class_id=1 AND cp.company_id=2 AND cp.start != 0 and (cp.end <= 10 or cp.start BETWEEN 0 and 10 )


Select  

(SELECT cp.price from vehicle_class_distance_price cp WHERE cp.vehicle_class_id=1 AND cp.company_id=2 AND cp.start >= 0 LIMIT 1) as FIRST_PRICE ,
(SELECT id FROM currency_rate WHERE from_currency_id = cp.currency_id AND to_currency_id = 141 LIMIT 1) as currency_rate_id,

(SELECT value FROM currency_rate_history WHERE rate_id = currency_rate_id ORDER BY id DESC LIMIT 1) as cur_price

FROM vehicle_class_distance_price cp 


WHERE cp.vehicle_class_id=1 AND cp.company_id=2 AND cp.start != 0 and (cp.end <= 500 or cp.start BETWEEN 0 and 500 ) 



Select

(SELECT id FROM currency_rate WHERE from_currency_id = cp.currency_id AND to_currency_id = 46 LIMIT 1) as currency_rate_id,
(SELECT value FROM currency_rate_history WHERE rate_id = currency_rate_id ORDER BY id DESC LIMIT 1) as cur_price,
(SELECT cp.price*cur_price from vehicle_class_distance_price cp WHERE cp.vehicle_class_id=1 AND cp.company_id=2 AND cp.start >= 0 LIMIT 1) as FIRST_PRICE
FROM vehicle_class_distance_price cp 


WHERE cp.vehicle_class_id=1 AND cp.company_id=2 AND cp.start != 0 and (cp.end <= 500 or cp.start BETWEEN 0 and 500 );

Select 
(SELECT id FROM currency_rate WHERE from_currency_id = cp.currency_id AND to_currency_id = 141 LIMIT 1) as currency_rate_id, 
(SELECT value FROM currency_rate_history WHERE rate_id = currency_rate_id ORDER BY id DESC LIMIT 1) as cur_price, 

SUM(CASE WHEN (cp.start <= 500 AND cp.end >= 500) THEN cp.price * cur_price * (500 - cp.start + 1) WHEN (cp.start <= 500 AND cp.end <= 500) THEN cp.price * cur_price * (cp.end - cp.start + 1) ELSE 0 END) AS TOTAL_PRICE, 

(SELECT cp.price * cur_price from vehicle_class_distance_price cp WHERE cp.vehicle_class_id=1 AND cp.company_id=2 AND cp.start >= 0 LIMIT 1) as FIRST_PRICE 

FROM vehicle_class_distance_price cp 
WHERE cp.vehicle_class_id=1 AND cp.company_id=2 AND cp.start != 0 and (cp.end <= 500 or cp.start BETWEEN 0 and 500 )



SELECT
vcte.id, vcte.name, vcte.price * crh.value, vcte.currency_id , crh.value, crh.rate_id, crh.date
FROM vehicle_class_to_extra vcte

LEFT JOIN currency_rate as cr ON from_currency_id = vcte.currency_id AND to_currency_id = 46 
LEFT JOIN currency_rate_history as crh ON crh.rate_id = cr.id AND crh.id = (SELECT id FROM currency_rate_history WHERE rate_id = cr.id ORDER BY id DESC LIMIT 1 )
WHERE vcte.company_id = 2 AND vcte.price != 0;



LEFT JOIN currency_rate_history as crh ON rate_id = cr.id

SELECT
vcte.id, vcte.name, vcte.price * crh.value, vcte.currency_id , crh.value
FROM vehicle_class_to_extra vcte

LEFT JOIN currency_rate as cr ON from_currency_id = vcte.currency_id AND to_currency_id = 46 
LEFT JOIN (SELECT value, rate_id FROM currency_rate_history ORDER BY id DESC LIMIT 1) as crh ON crh.rate_id = cr.id
LEFT JOIN currency_rate_history as crh ON rate_id = cr.id


SELECT
(SELECT id FROM currency_rate WHERE from_currency_id = vcte.currency_id AND to_currency_id = 147  LIMIT 1) as currency_rate_id,
        (SELECT value FROM currency_rate_history WHERE rate_id = currency_rate_id ORDER BY id DESC LIMIT 1) as cur_price,

vcte.id, vcte.name, vcte.price*cur_price, vcte.currency_id 
FROM `vehicle_class_to_extra` vcte WHERE `company_id` = 2 AND `price` != 0;



Select 

SUM(CASE 
WHEN (cp.start <= 500 AND cp.end >= 500) THEN cp.price * crh.value * (500 - cp.start + 1) 
WHEN (cp.start <= 500 AND cp.end <= 500) THEN cp.price * crh.value * (cp.end - cp.start + 1) 
ELSE 0 END) AS TOTAL_PRICE,
 (SELECT cp.price * crh.value from vehicle_class_distance_price cp WHERE cp.vehicle_class_id=1 AND cp.company_id=2 AND cp.start >= 0 LIMIT 1) as FIRST_PRICE 
 
 FROM vehicle_class_distance_price cp
 LEFT JOIN currency_rate as cr ON from_currency_id = cp.currency_id AND to_currency_id = 147 
LEFT JOIN currency_rate_history as crh ON rate_id = cr.id
 
  WHERE cp.vehicle_class_id=1 AND cp.company_id=2 AND cp.start != 0 and (cp.end <= 500 or cp.start BETWEEN 0 and 500 )