Hibernate, how to count with condition
I just solved the problem with the following code.
select sum(case when st.averageMark >= su.gradePass then 1 else 0 end) as pass,
sum(case when st.averageMark < su.gradePass then 1 else 0 end) as fail
from Study st join st.subject su
where st.acaYear in (2009) and st.semester = 4 and su.idSeq = 1330
group by st.acaYear
Combining Nathanphan's answer and M. A. Khomeni's comment,
CASE
is not supported inCOUNT()
So we need to use SUM()
instead of COUNT()
For Example:
COUNT(CASE WHEN st.averageMark < su.gradePass THEN 1 ELSE 0 END)
Should be written as
SUM(CASE WHEN st.averageMark < su.gradePass THEN 1 ELSE 0 END)