2010-11-12 47 views
2

HQL是否支持集合函數中的條件表達式?集合函數中的hql條件表達式

我願做這樣的事情

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

但我從ANTLR的解析器MissingTokenException。

編輯:通過使用子查詢它的工作,但它的醜陋,因爲我受到幾個變量的分組...

回答

2

可以在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相同的功能。