使用HAVING时出现“无效的列名" [英] 'invalid column name' while using the HAVING

查看:513
本文介绍了使用HAVING时出现“无效的列名"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Microsoft SQL SERVER 2014.

I am using Microsoft SQL SERVER 2014.

以下是我的查询

SELECT type, SUM(calories) AS total_calories
FROM exercise_logs
GROUP BY type
HAVING total_calories > 150;

我得到了错误

第207级,状态1,第2行,Msg 无效的列名"total_calories".

Msg 207, Level 16, State 1, Line 2 Invalid column name 'total_calories'.

这是一个非常简单的表(我是sql的新手,正在学习它).有人可以指出我在做什么错吗?谢谢.

it is a very simple table (I am new to sql and learning it). can somebody point out what am i doing wrong? Thanks.

推荐答案

Aggregation是必需的,因为您无权访问别名total_calories

Aggregation is required , as you have no access to alias total_calories

SELECT   type,SUM(calories) AS total_calories 
FROM     exercise_logs 
GROUP BY type 
HAVING   SUM(calories) > 150;

这篇关于使用HAVING时出现“无效的列名"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆