Jpa multiple counters

I have a table A with columns x, y and z. I want to make a query to count the rows where (Ax = 1 and Ay = 4), (Ax = 2 and Ay = 7) and (Ax = 3 and Ay = 3). The result should consist of 3 integers matching the above criteria.

I do not know if such a request is possible.

I need sample code in JPA criteria (preferably) or JPQL or, in the worst case, SQL.

+4
source share
1 answer

Here is your worst case scenario:

SELECT SUM(CASE WHEN ax = 1 AND ay = 4 THEN 1 ELSE 0 END) AS Result1, SUM(CASE WHEN ax = 2 AND ay = 7 THEN 1 ELSE 0 END) AS Result2, SUM(CASE WHEN ax = 3 AND ay = 3 THEN 1 ELSE 0 END) AS Result3 FROM TableA a 
+2
source

Source: https://habr.com/ru/post/1395117/


All Articles