如何在where子句中使用聚合函数

时间:2016-07-19 05:55:22

标签: sql sql-server max

我在where子句中使用聚合函数时遇到错误。

  

'聚合可能不会出现在where子句中,除非它是a   包含在having子句或选择列表中的子查询以及列   被汇总是一个外部参考'。

查询:

Select a.*,b.* 
from address a 
join account c on a.acct_no=b.acct_no 
where a.stop_date in (select max(a.stop_date) 
                      from address x 
                      where x.acct_no=a.acct_no and x.addr_code=a.addr_code)

请建议如何处理

1 个答案:

答案 0 :(得分:0)

您应该使用x.stop_date代替a.stop_date

Select a.*,b.* 
from address a 
join account b on a.acct_no=b.acct_no 
where a.stop_date in (select max(x.stop_date) 
                      from address x 
                      where x.acct_no=a.acct_no and x.addr_code=a.addr_code)
相关问题