Ver originalselect id_comanda, codi_comanda, comandes.descripcio, data, if(length(factura),factura,'*'), clients.nom, estat, ROUND((coalesce(hores_afegides,0)+coalesce(hores,0)),2) as total_hores, ROUND((coalesce(materials_afegits,0)+coalesce(materials,0)),2) as total_materialsfrom comandesleft join (select id_comanda, sum(quantitat) as hores_afegides from comandes_families where id_familia=7 group by id_comanda)T1 using (id_comanda)left join (select id_comanda, sum(quantitat) as hores from comandes_albarans_families where id_familia=7 group by id_comanda)T2 using (id_comanda)left join (select id_comanda, sum(quantitat*multiplicatiu) as materials_afegits from comandes_families where id_familia!=7 group by id_comanda)T3 using (id_comanda)left join (select id_comanda, sum(quantitat*multiplicatiu) as materials from comandes_albarans_families where id_familia!=7 group by id_comanda)T4 using (id_comanda)inner join clients using (id_client)inner join estats using (id_estat)