1.Having子句后面要跟布尔类型的表达
2.别名branch_avg后的(branch_name,avgbalance)也是多余的。
修改后:
select branch_name,avgbalance
from (select branch_name,avg(balance) as avgbalance
from account_table
group by branch_name)
as branch_avg
group by branch_name
having max(avgbalance) = sum(avgbalance)
也可以这样写:
将平均balance降序排序,第一行就是最大的。
select top 1 branch_name,avgbalance
from (select branch_name,avg(balance) as avgbalance
from account_table
group by branch_name)
as branch_avg
order by avgbalance desc
把这段语句放在查询分析器里检查一下就清楚了