我不像SQL Server那样熟悉MySQL,但在T-SQL中,你不能在GROUP BY子句中使用别名(我最初也认为ORDER BY子句,但后来证明这是不正确的) .在这种情况下,您希望根据GROUP BY的结果进行过滤,因此我将使用HAVING子句,如下所示:
SELECT *, SUM(... some SQL removed for brevity ...) AS Occurrences FROM some_table AS q WHERE criterion = 'value' GROUP BY q.P_id HAVING SUM(... some SQL removed for brevity ...) > 0 ORDER BY Occurrences DESC LIMIT 10;