Search code examples
jpaspring-datanativequery

JPA Custom @Query for counting !boolean


I am doing a lot of arithmetic and joins in my actual Query, all of which are working fine.

I have the following (Simplified version for the purpose of this question) working custom query, where i am counting the number of instances with 'isSuccess == true':

@Query("SELECT NEW com.xyz.MyEntity(S.name, SUM(S.isSuccess)) FROM Sample AS S")
public List<MyEntity> caclulateResults();

However I would like to count the 'S.isSuccess = false' instead and I am curious about what would be the easiest way to achieve this? The JPA documentation is of not much help here. Thank you!


Solution

  • I resolved this by doing.

    SUM(CASE S.isSuccess WHEN true THEN 0 ELSE 1 END)