开发者

hql conditional expressions in aggregate functions

Does HQL support conditional expressions in aggregate functions?

I would like to do something like this

select
  o.id, 
  count(o),
  count(o.something is null and o.othert开发者_如何学Gohing = :other)
from objects o

But I get a MissingTokenException from the Antlr parser.

EDIT: By using a subquery it's working, but it's ugly as I'm grouping by several variables...


You can use expressions in HQL. For this instance, you'll want to use SUM instead of COUNT as follows:

select
  o.id, 
  count(o),
  sum(case when o.something is null and o.otherthing = :other then 1 else 0 end)
from objects o

When the conditionals match, the SUM will receive a 1 for that row. When they do not match, it'll receive a zero. This should provide the same functionality as a COUNT.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜