传递参数以在HQL中选择具有group by的案例

时间:2010-10-06 16:06:16

标签: oracle hibernate hql

我有以下HQL( Hibernate查询语言)查询(在oracle数据库连接上执行)以检索基于月份的聚合总和

select sum(doc._masskg), 
    case when (month(doc._date) = month(:m)) then 'NOW' else 'BEFORE' 
from Document doc 
    where month(doc._date) <= month(:m) 
group by 
    case when (month(doc._date) = month(:m)) then 'NOW' else 'BEFORE' end

但是,在运行时我得到Oracle异常NOT A GROUP BY EXPRESSION

如果我将:m 替换为 sysdate ,则查询有效。 如果m:是Date?

,如何重写查询

1 个答案:

答案 0 :(得分:0)

所以,我最终用实际值替换了参数:

String hql = "select sum(doc._masskg), "+
"    case when (month(doc._date) = month(%1$s)) then 'NOW' else 'BEFORE' "+
"from Document doc "+
"    where month(doc._date) <= month(%1$s) "+
"group by "+
"    case when (month(doc._date) = month(%1$s)) then 'NOW' else 'BEFORE' end";

Iterator i = session.createQuery(String.format(HQL, "to_date('09.10.2011', 'DD.MM.YYYY')"))
                .iterate();