在加入期间,我得到下表(示例):
+----------+----------+
| Hostname | Severity |
+----------+----------+
| host1 | high |
| host2 | medium |
| host1 | high |
| host2 | low |
| host1 | low |
| host2 | low |
| host1 | low |
| host2 | high |
| host1 | high |
| host2 | high |
+----------+----------+
是否可以创建 JPQL 查询并获得以下结果:
+----------+------+--------+-----+
| Hostname | high | medium | low |
+----------+------+--------+-----+
| host1 | 3 | 0 | 2 |
| host2 | 2 | 1 | 2 |
+----------+------+--------+-----+
我尝试使用 COUNT
和 GROUP BY
但我得到了类似的结果:
host1,high,3
host1,medium,0
host1,low,2
等等...
BR,雷内
最佳答案
标准的 sql 查询看起来像这样 - 我不确定您正在使用的 API 需要付出什么努力,但它是一个非常简单的带有案例逻辑的分组。
select hostname,
sum(case when severity = 'high' then 1 else 0 end) as high,
sum(case when severity = 'medium' then 1 else 0 end) as medium,
sum(case when severity = 'low' then 1 else 0 end) as low
from
Table
group by
hostname
order by
hostname
关于Java Hibernate JPQL 查询(聚合函数 : count),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7064618/