SELECT DATE_FORMAT(LAST_DAY(LEFT(f.dateSysteme,10)), "%d/%m/%Y") AS PeriodeSoins,
f.codePrestataire,fn_libelle_prestataire (f.codePrestataire) AS prestataire,-- lbc_prestat,
f.idBeneficiaire,f.numeroBeneficiaire,CONCAT (b.nomBeneficiaire," ",b.prenomsBeneficiaire) AS beneficaire,
f.dateConsultation,-- codeActe,
-- DATE_FORMAT(LAST_DAY(LEFT(dateSysteme,10)), "%d/%m/%Y") AS dateComptable,
f.montantArembourser,CASE WHEN f.tiersPayant="1" THEN "TP" ELSE "RD" END AS tiersPayant, COUNT(*)  ASquantite
FROM p_feuillemaladie f
INNER JOIN beneficiaire b ON ( b.id=f.idBeneficiaire )

WHERE (LEFT(f.dateSysteme,10) BETWEEN '2022-01-01' AND '2022-12-31')
AND f.montantArembourser>0
--  idPolice
GROUP BY f.codePrestataire,f.idBeneficiaire,f.numeroBeneficiaire,
f.dateConsultation,f.dateSysteme,f.montantArembourser,f.tiersPayant  -- codeActe,
HAVING COUNT(*) > 1
ORDER BY PeriodeSoins,f.idBeneficiaire DESC