我正在使用Microsoft SQL SERVER 2014.
以下是我的查询
SELECT type, SUM(calories) AS total_calories FROM exercise_logs GROUP BY type HAVING total_calories > 150;
我得到了错误
消息207,级别16,状态1,行2无效的列名称'total_calories'.
这是一个非常简单的表(我是sql和学习它的新手).谁能指出我做错了什么?谢谢.
Aggregation
是必需的,因为您无法访问别名 total_calories
SELECT type,SUM(calories) AS total_calories FROM exercise_logs GROUP BY type HAVING SUM(calories) > 150;