HQL支持聚合函数中的条件表达式吗?
我想做这样的事情
select
o.id,
count(o),
count(o.something is null and o.otherthing = :other)
from objects o
但是我从 Antlr 解析器收到 MissingTokenException。
编辑:通过使用子查询,它可以工作,但它很丑陋,因为我按多个变量进行分组...
最佳答案
您可以在 HQL 中使用表达式。对于本例,您需要使用 SUM 而不是 COUNT,如下所示:
select
o.id,
count(o),
sum(case when o.something is null and o.otherthing = :other then 1 else 0 end)
from objects o
当条件匹配时,该行的 SUM 将收到 1。当它们不匹配时,它将收到零。这应该提供与 COUNT 相同的功能。
关于nhibernate - 聚合函数中的 hql 条件表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4164629/