如何在休眠状态下编写SQL sum()和groupby?

时间:2018-09-24 10:17:49

标签: hibernate hibernate-criteria criteria-api

我的查询是:

select GRN_NO, IDENTIFICATION_NO, RBI_TRN_DATE, PERIOD_FROM, PERIOD_TO,
       SUM(AMOUNT) AS AMOUNT
from PTAX_ECHALLAN_DATA
where IDENTIFICATION_NO in ('ECC0013056', '192009028150', '20150086699') 
GROUP BY GRN_NO;

我正在尝试使用以下代码:

Criteria cr = session.createCriteria(PTAX_ECHALLAN_DATA.class);
cr.setProjection(Projections.projectionList()
.add(Projections.sum("amount").as("amount"))
.add(Projections.property("rbiTrnDate").as("rbiTrnDate"))
.add(Projections.property("idNo").as("idNo"))
.add(Projections.property("grnDate").as("grnDate"))
.add(Projections.property("preiodTo").as("preiodTo"))
 .add(Projections.property("preiodFrm").as("preiodFrm"))
 .add(Projections.groupProperty("id.grnNO")));
 cr.add(Restrictions.in("idNo", Arrays.asList(OldEc, EC, applnNo)));
cr.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
List list = cr.list();

但是会出错。

  

错误为:“,由以下原因引起:java.sql.SQLSyntaxErrorException:ORA-00979:   不是GROUP BY表达式”。

我该怎么写?请帮忙。预先感谢。

1 个答案:

答案 0 :(得分:1)

您需要选择“分组依据”中除聚合列之外的所有其他电量

select GRN_NO, IDENTIFICATION_NO, RBI_TRN_DATE, PERIOD_FROM, PERIOD_TO,
       SUM(AMOUNT) AS AMOUNT
from PTAX_ECHALLAN_DATA
where IDENTIFICATION_NO in ('ECC0013056', '192009028150', '20150086699') 
GROUP BY GRN_NO, IDENTIFICATION_NO, RBI_TRN_DATE, PERIOD_FROM, PERIOD_TO