总和上的列名无效

时间:2011-06-23 11:56:51

标签: sql sql-server

我正在尝试执行sql语句

select buyer, issuer, ident, sum(qnty) as sum1
from palete
where sum1 <> 0
group by buyer, issuer, ident

我收到了错误 列名称“sum1”无效。

问题是我得到了很多结果,所以我想在sum1为0的情况下避免这些结果。

感谢您的帮助!

4 个答案:

答案 0 :(得分:6)

替代,没有子查询:

select buyer, issuer, ident, sum(qnty) as sum1 
from palete 
group by buyer, issuer, ident 
having sum(qnty) <> 0

答案 1 :(得分:1)

别名在where子句中不被识别。你需要这样做:

select buyer, issuer, ident, sum(qnty) as sum1 
from palete 
group by buyer, issuer, ident 
having sum(qnty) <> 0

答案 2 :(得分:0)

select * from
(
  select buyer, issuer, ident, sum(qnty) as sum1 from palete group by buyer, issuer, ident
) as T1 where T1.sum1 <> 0

答案 3 :(得分:0)

您需要使用HAVING子句而不是WHERE子句。

SQL HAVING子句与SELECT子句一起使用,以指定组或聚合的搜索条件。 HAVING子句的行为类似于WHERE子句,但适用于组 - 结果集中表示组的行。相反,WHERE子句适用于单个行,而不适用于组。

希望这有帮助。