INSERT INTO animal_movements(animal_id,kind,place,lieu,note,created_at) SELECT a.id,'entry',COALESCE(NULLIF(trim(a.rescue_location_name),''),NULLIF(trim(a.rescue_address),'')), 'Entrée historique','Informations d’entrée à compléter dans la fiche animale', COALESCE(NULLIF(a.intake_date,'')||' 12:00:00',a.created_at,datetime('now')) FROM animals a WHERE NOT EXISTS(SELECT 1 FROM animal_movements m WHERE m.animal_id=a.id AND m.kind='entry');