ho dato solo un'occhiata veloce, ma il problema dovrebbe essere il COUNT(),