# How to resolve "Subquery containing correlated aggregate function can only appear in having or select clause"

**URL:** <https://discourse.getdbt.com/t/how-to-resolve-subquery-containing-correlated-aggregate-function-can-only-appear-in-having-or-select-clause/5031>\
**Category:** Show and Tell\
**Tags:** incremental, snowflake\
**Created:** [October 7, 2022, 5:44pm UTC](https://discourse.getdbt.com/t/how-to-resolve-subquery-containing-correlated-aggregate-function-can-only-appear-in-having-or-select-clause/5031 "2022-10-07T17:44:19Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![sactl](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@sactl](https://discourse.getdbt.com/u/sactl)\
**Post date:** [December 12, 2023, 9:47am UTC](https://discourse.getdbt.com/t/how-to-resolve-subquery-containing-correlated-aggregate-function-can-only-appear-in-having-or-select-clause/5031/13 "2023-12-12T09:47:09Z")

</div>

Hello guys,

I have the same problem as mentionned. error: Subquery containing correlated aggregate function [MAX(INTER\_ORDERED\_PRODUCT.ORDERDATE)] can only appear in having or select clause

I have tried the solution that the community propose but still i encounter an error.

Context i’m using snowflake and dbt cloud.

best,

here what i did:  
{{ config(schema=‘Marts’) }}  
{{ config(materialized=“incremental”) }}

with mart\_ordered\_product as (  
select \* from {{ref (“inter\_ordered\_product”)}}  
),

final as (  
select  
orderedkey,  
partkey,  
suppkey,  
linenumber,  
quantity,  
extendedprice,  
discount,  
tax,  
returnflag,  
linestatus,  
shipdate,  
commitdate,  
receitdate,  
shipinstruct,  
shipmode,  
li\_comment,  
customer\_key,  
orderstatus,  
totalprice,  
orderdate,  
orderpriority,  
clerk,  
shipriority,  
o\_comment  
from mart\_ordered\_product  
{%if is\_incremental() %}  
where mart\_ordered\_product.orderdate \>= (select max(this.orderdate) from {{this}} as this)  
{% endif%}  
)

select \* from final

---

_[View the full topic](https://discourse.getdbt.com/t/how-to-resolve-subquery-containing-correlated-aggregate-function-can-only-appear-in-having-or-select-clause/5031)._
