-- Script pour trouver les contrats dont le champ "structure" ne correspond pas au nom de l'organisation -- Cela permet d'identifier les incohérences dans les données SELECT c.id, c.contract_number, c.employee_name, c.structure AS structure_actuelle, o.name AS nom_organisation, c.start_date, c.end_date, c.created_at FROM cddu_contracts c INNER JOIN organizations o ON c.org_id = o.id WHERE c.structure IS DISTINCT FROM o.name -- IS DISTINCT FROM gère aussi les cas NULL ORDER BY c.created_at DESC; -- Compter le nombre total de contrats avec incohérence SELECT COUNT(*) as nombre_contrats_incoherents FROM cddu_contracts c INNER JOIN organizations o ON c.org_id = o.id WHERE c.structure IS DISTINCT FROM o.name; -- Grouper par organisation pour voir quelles organisations ont le plus d'incohérences SELECT o.name AS nom_organisation, COUNT(*) as nombre_incoherences, ARRAY_AGG(DISTINCT c.structure) as structures_utilisees FROM cddu_contracts c INNER JOIN organizations o ON c.org_id = o.id WHERE c.structure IS DISTINCT FROM o.name GROUP BY o.name ORDER BY nombre_incoherences DESC;