Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to check more than two conditions in having clause?

When I am trying the below query

select column2 
from table1 
group by column2 
having count(column3) = count(column1) and column5 in (1,2)

I am getting this error:

ORA-00979: not a GROUP BY expression
00979. 00000 -  "not a GROUP BY expression"

What should I write in order to get result i.e. column2 which satisfies both the conditions

i.e. (count(column3) = count(column1) and column5 in (5,6).

like image 298
pooja rajput Avatar asked Dec 09 '25 07:12

pooja rajput


2 Answers

Try this:

select column2
from table1
where column5 in (1,2)
group by column2
having count(column3) = count(column1)
like image 103
Jens Avatar answered Dec 11 '25 20:12

Jens


You actually have use part that should be in where condition and not in having as below.

SELECT column2 
FROM table1 
WHERE column5 IN (1,2) 
GROUP BY column2 
HAVING COUNT(column3) = COUNT(column1)

Always use aggregate functions in having clause to do the validation within query and not in clause.

like image 34
SMA Avatar answered Dec 11 '25 20:12

SMA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!