DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`fruitsEvolution`() BEGIN select Id_Cliente, Cliente, count(semana) as semanas, (w.code IS NOT NULL) isWorker from ( select distinct v.Id_Cliente, c.name as Cliente, week(fecha, 3) as semana from bs.ventas v join vn.itemType it ON it.id = v.tipo_id join vn.client c ON c.id = v.Id_Cliente where it.categoryFk = 12 and v.fecha > '2020-01-01') sub left join vn.worker w ON w.id = sub.Id_Cliente group by Id_Cliente; END$$ DELIMITER ;