The same query you use when usage_type is by_cost, maybe define it before last one if construction? This is common part of code.