There is a table injections

enter image description here

there is a select request

I need to make it so that if date is the previous month, then estimate_usage_rate = usage_rate otherwise just injections.estimate_usage_rate as estimate_usage_rate

sketched until such a pseudocode

 IF(DATE_FORMAT(injections.date,'%Y-%m') == CONCAT(EXTRACT(YEAR FROM NOW()), '-', EXTRACT(MONTH FROM NOW( ) ) -1, '%' )) THEN estimate_usage_rate = usage_rate ELSE injections.estimate_usage_rate as estimate_usage_rate END IF; 
  • And by the way, what does your design mean injections.estimate_usage_rate as estimate_usage_rate is called columns in queries, but if / end_if it is clearly not appropriate - Mike
  • well, I would probably describe the condition itself as extract(year_month from injections.date)=extract(year_month from now()-interval 1 month) but what is AS, what should I do, I can’t imagine - Mike
  • as does not matter, my requests just go through the aliases of production.avg_gas as avg_gas, injections.cost_centre, etc @Mike - gudfar
  • here the matter is how to reassign it under the condition - gudfar
  • 2
    Well, in your case the same case is already used, by analogy with the case when extract..=eaxtract then usage_rate else injections.estimate_usage_rate end as estimate_usage_rate - Mike

1 answer 1

select case when MONTH( date ) = MONTH(DATE_ADD(NOW(), INTERVAL -1 MONTH)) AND YEAR( date ) = YEAR(NOW()) then injections.usage_rate else injections.estimate_usage_rate end as estimate_usage_rate from injections