您好我使用sql server并且在尝试执行以下查询时遇到此错误:
select
e.dept_id, count(e.dept_id) as empCount from employee e
group by e.dept_id
having empCount > 0;
无效的列名'empCount'。
当我这样写时:
having e.dept_id > 0;
它有效,但我想用别名empCount
替换它。
答案 0 :(得分:1)
使用如下所示
select
e.dept_id, count(e.dept_id) as empCount from employee e
group by e.dept_id
having count(e.dept_id) > 0;
答案 1 :(得分:0)
将GROUP BY部分包装在派生表中,然后可以使用empCount:
select dept_id, empCount
from
(
select e.dept_id, count(e.dept_id) as empCount
from employee e
group by e.dept_id
)
where empCount > 0