select f.farm_name,
c.crop_name,
c.crop_type,
round(sum(d.health_index) / count(c.crop_name), 2) avg_health_index,
round(max(d.yield_kg_per_hectare)) avg_yield_kg_per_hectare,
datediff(max(d.measurement_date), min(d.planting_date)) growth_days
from crop_data d
join crops c on d.crop_id = c.crop_id
join farms f on d.farm_id = f.farm_id
where d.measurement_date between '2024-03-01' and '2024-06-30'
group by f.farm_name, c.crop_name, c.crop_type, c.crop_name
having count(d.measurement_date) >= 3
order by avg_health_index desc, avg_yield_kg_per_hectare desc, f.farm_name;