【问题标题】:Hibernate Native query returned duplicated result in result array with group by added in queryHibernate Native 查询在结果数组中返回了重复的结果,并在查询中添加了 group by
【发布时间】:2017-11-26 15:47:29
【问题描述】:

我使用的是 Hibernate 版本 3。 我的表有 9 列,我的查询看起来像

select col1, count(col2), sum(col3), sum(col4) from table a
 where a.col5= 'criteria1' and a.col6 = 'criteria2' 
 and a.col7 = 'criteria3' and a.col8 = 'criteria4' group by col9

col2 是表的 id 和主键。 当我在查询浏览器中运行相同的查询时,它会给我正确的结果。 例如,如果浏览器返回的结果是 test,10,300,500 natove查询返回的结果不一样,是test,10,10,10

在本机查询结果中,结果中的第二列被复制到第三和第四列。 仅当 group by 添加到查询时才观察到此行为。如果我删除分组依据并对列求和,它会返回正确的结果。我在谷歌上研究过似乎存在一个问题,但没有找到答案。任何帮助将不胜感激

【问题讨论】:

    标签: sql-server hibernate jpa hibernate-native-query


    【解决方案1】:

    countsum 添加别名解决了这个问题。

    固定查询如下所示。更改以粗体显示。

    select col1, count(col2) as count1, sum(col3) as sum1, sum(col4) as sum2 from table a
    where a.col5= 'criteria1' and a.col6 = 'criteria2' 
    and a.col7 = 'criteria3' and a.col8 = 'criteria4' group by col9
    

    【讨论】:

      猜你喜欢
      • 2019-02-06
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多