How to combine in one condition HAVING and AND in Oracle

273 Views Asked by At

I wonder how can I put in one condition having and for example:

select count(s.id) from card c
                  join subject s on c.ean=s.ean
                  join account a on s.id=a.owner_subject
                 where a.status_external=0
                   and a.status_internal=0
                   and s.type=0
having( 
( trunc(max(s.hist_modified_tmsp))<=last_day(add_months(trunc(sysdate,'mm'),-1)) and level=1 )  
or  
( trunc(min(s.hist_modified_tmsp))<=last_day(add_months(trunc(sysdate,'mm'),-1)) and level=2 ) )

My Oracle tells me "Not a GROUP BY Expression". Any ideas please? I will be very grateful for any help, thank you

1

There are 1 best solutions below

6
On BEST ANSWER

Having begins a clause similar to where (except applied after aggregation (e.g. sums) instead of before), so you can only have it once in the query like this:

having ((sum(col)<=30 and type=4) or (sum(col)>=30 and type=5))

Also you need a group by clause specifying the columns that the query groups by:

group by type, ...