Doubts in HQL while doing Sum function

0

I have a question in hql.

My system has 3 tables table1 and table 2 and table 3

table 3 stores the primary key records of table1 and table2 to make relationships.

Table 1 can contain N associations with table 2 but table 2 can only belong to 1 table 1 record;

I want to make a select that returns the sum of a field that stores an integer, a date from table2, and the id of table1.

When I run this Hql it only works correctly if "b.field" is unique otherwise it does not execute the sum function properly since no select.

select distinct a.id,sum(b.valor) ,b.campo from entidade1 a inner join entidade2 b;

I know this happens because all the different values are returned at the time of summing, but I do not know how to correct it. Do you have any way?

    
asked by anonymous 15.03.2018 / 19:25

1 answer

0

Friend, to sum sum() you need to group group by .

I think your query would look like this:

select a.id,sum(b.valor) ,b.campo from entidade1 a inner join entidade2 b GROUP BY a.id, b.campo

With the GROUP BY clause you do not need distinct .

Here is the documentation for Hibernate :

  

link

    
15.03.2018 / 22:21