gpt4 book ai didi

nhibernate - 聚合函数中的 hql 条件表达式

转载 作者:行者123 更新时间:2023-12-04 06:41:38 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com