sql – JPA多重计数

我有一个表格,其中包含x,y和z列.我想做一个查询来计算行(A.x = 1和A.y = 4),(A.x = 2和A.y = 7)和(A.x = 3和A.y = 3)的行.结果应包含3个整数,对应于上述标准.

我不知道这种查询是否可行.

我想在JPA Criteria(最好)或JPQL中使用示例代码,或者在最坏的情况下使用SQL.

最佳答案 这是你最糟糕的情况:

SELECT
    SUM(CASE WHEN a.x = 1 AND a.y = 4 THEN 1 ELSE 0 END) AS Result1,
    SUM(CASE WHEN a.x = 2 AND a.y = 7 THEN 1 ELSE 0 END) AS Result2,
    SUM(CASE WHEN a.x = 3 AND a.y = 3 THEN 1 ELSE 0 END) AS Result3
FROM TableA a
点赞