代码之家  ›  专栏  ›  技术社区  ›  simendsjo

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

  •  2
  • simendsjo  · 技术社区  · 15 年前

    HQL在聚合函数中支持条件表达式吗?

    select
      o.id, 
      count(o),
      count(o.something is null and o.otherthing = :other)
    from objects o
    

    但是我从Antlr解析器中得到一个MissingTokenException。

    编辑:

    1 回复  |  直到 15 年前
        1
  •  3
  •   Shane N    14 年前

    可以在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
    

    当条件符匹配时,该行的和将收到1。当它们不匹配时,将得到零。这应该提供与COUNT相同的功能。