truncate ordenes_facts;
Insert into ordenes_facts 
SELECT  null as id,laboratorios.laboratorio as laboratorio ,cias.nombre as cia, trim(both from doctores.tmpllave) as medico,
	concat_ws('-', year(fechares),month(fechares),'01') as fecha, datosexa.status as status,
	count(numerex) as cuantos, sum(totalpagar) as totalpagar, sum(pagado) as pagado, sum(datosexa.saldo) as saldo,
	sum(totalpagarcli) as totalpagarcli, sum(pagadocli) as pagadocli, sum(saldocli) as saldocli,
	sum(totalpagarcia) as totalpagarcia, sum(pagadocia) as pagadocia, sum(saldocia) as saldocia
	FROM datosexa 
		JOIN laboratorios ON (datosexa.labid=laboratorios.labid) 
		JOIN cias ON (datosexa.cia=cias.codigo) 
		JOIN doctores ON (datosexa.medico = doctores.codigo)  
	GROUP BY 2,3,4,5,6;
