统计HQL Query中的组数?
Count the number of groups in HQL Query?
我有以下 HQL 查询:
select c.device from Choice c
group by c.device
现在我想计算结果中的组数,而不是每组的设备数。
我试过:
select count(distinct c.device) from Choice c
group by c.device
但这给出了每组中不同设备的数量。这类似于 [2,3,4]
。但是我需要2+3+4
。
如何使用 HQL 获取组数?
你必须做一个计数,这在 HQL 中是不支持的。
SQL
在 SQL 中看起来像这样:
select count(innerQuery.counted)
from (select count(d.id) as counted
from Choice c inner join Device d
on c.device_id = d.id
group by d.id) as innerQuery
在上面的示例中,外部查询从 returns 组的子查询中进行选择。然后外部查询对生成的 counted
列进行计数。
HQL
另一种方法是进行计数,然后获取列表的大小。
Choice.executeQuery('select count(c.device) from Choice c group by c.device').size()
因为计数是在客户端完成的,所以会有性能损失。
我有以下 HQL 查询:
select c.device from Choice c
group by c.device
现在我想计算结果中的组数,而不是每组的设备数。
我试过:
select count(distinct c.device) from Choice c
group by c.device
但这给出了每组中不同设备的数量。这类似于 [2,3,4]
。但是我需要2+3+4
。
如何使用 HQL 获取组数?
你必须做一个计数,这在 HQL 中是不支持的。
SQL
在 SQL 中看起来像这样:
select count(innerQuery.counted)
from (select count(d.id) as counted
from Choice c inner join Device d
on c.device_id = d.id
group by d.id) as innerQuery
在上面的示例中,外部查询从 returns 组的子查询中进行选择。然后外部查询对生成的 counted
列进行计数。
HQL
另一种方法是进行计数,然后获取列表的大小。
Choice.executeQuery('select count(c.device) from Choice c group by c.device').size()
因为计数是在客户端完成的,所以会有性能损失。