INSERT INTO master_ternak (eartag, created_by)
SELECT TRIM(mt.eartag), MAX(mt.created_by)
FROM monitoring_ternak mt
JOIN monitoring_kandang mk ON mk .id = mt.monitoring_kandang_id
GROUP BY TRIM(eartag);