简体   繁体   中英

How to use aggregate function in where clause

I'm getting below error while using aggregate function in where clause.

'An aggregate may not appear in the where clause unless it is a subquery contained in a having clause or a select list, and the column being aggregated is an outer referrence'.

Query :

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)

Please suggest how to deal with it

You should use x.stop_date instead of 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM