# Wrong sum aggregation with search on related fields

**URL:** <https://forum.servoy.com/t/wrong-sum-aggregation-with-search-on-related-fields/9123>\
**Category:** Classic Servoy\
**Created:** [May 26, 2008, 8:53am UTC](https://forum.servoy.com/t/wrong-sum-aggregation-with-search-on-related-fields/9123 "2008-05-26T08:53:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![boonstrj](https://avatars.discourse-cdn.com/v4/letter/b/9d8465/32.png) [@boonstrj](https://forum.servoy.com/u/boonstrj)\
**Post date:** [May 26, 2008, 8:53am UTC](https://forum.servoy.com/t/wrong-sum-aggregation-with-search-on-related-fields/9123/1 "2008-05-26T08:53:12Z")

</div>

I have a form based on table A which has an one to many relation with table B. On table A a sum aggregation is defined. The form is a table view with the sum aggregation in the footer section. When I do a find and search on a related field from table B. The form shows all the records from table A which have at least one related record in table B which satisfies the search condition. So far, so good. But the sum aggregation of the field in table A doesn’t show the sum of the shown records, which I expected. It shows a higher value if records from table A have more than one record in table B which satisfies the search condition.

Is this correct behavior or a bug ? Any suggestions on getting the right sum.

---

<div class="post-metadata">

**Author:** ![Hans\_Nieuwenhuis](https://avatars.discourse-cdn.com/v4/letter/h/87869e/32.png) [@Hans\_Nieuwenhuis](https://forum.servoy.com/u/Hans_Nieuwenhuis)\
**Post date:** [May 26, 2008, 9:25am UTC](https://forum.servoy.com/t/wrong-sum-aggregation-with-search-on-related-fields/9123/2 "2008-05-26T09:25:15Z")

</div>

If you use the same relation for the calculation as you use for displaying the records, it should work.

I have a simmular application, but i always want to see the real total  
(so also including records not shown due to a find/search).  
I do this by creating a different relation (different name, the rest is the same)

I use calculations and not aggregations, but i expect it works the same way.

Regards,

Hans

---

<div class="post-metadata">

**Author:** ![boonstrj](https://avatars.discourse-cdn.com/v4/letter/b/9d8465/32.png) [@boonstrj](https://forum.servoy.com/u/boonstrj)\
**Post date:** [May 26, 2008, 10:15am UTC](https://forum.servoy.com/t/wrong-sum-aggregation-with-search-on-related-fields/9123/3 "2008-05-26T10:15:51Z")

</div>

Thanks for the reply, but I’m not using the relation to show things.  
Maybe an example will clarify things:

Table A:  
PK\_A | Field1 | Field2  
1 | First | 100  
2 | Second | 50

Table B:  
PK\_B | FK\_A | Field1  
1 | 1 | Test one  
2 | 1 | Test two  
3 | 2 | Test one

Table A has sum aggregation on Field2: SumField2  
And relation one to many from table A to B: relAtoB (PK\_A - FK\_A)

## Show all records of table A gives: 1 First 100 2 Second 50

SumField2 150

## With find on Field1 = ‘First’ gives: 1 First 100

SumField2 100

## With find on relAtoB.Field1 = ‘Test one’ gives: 1 First 100 2 Second 50

SumField2 150

## All seems Ok, but find on relAtoB.Field1 = ‘Test%’ gives: 1 First 100 2 Second 50

SumField2 250 !!

* * *

Servoy Developer  
Version 3.5.6-build 519  
Java version 10.0-b19 (Windows XP)

---

<div class="post-metadata">

**Author:** ![jcompagner](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/jcompagner/32/4889_2.png) [@jcompagner](https://forum.servoy.com/u/jcompagner)\
**Post date:** [June 30, 2008, 10:21am UTC](https://forum.servoy.com/t/wrong-sum-aggregation-with-search-on-related-fields/9123/4 "2008-06-30T10:21:05Z")

</div>

This is how sql works

your sum query is something like this:

select sum(tableb.Field2) from tablea, tableb where tablea.pk\_a = tableb.fk\_a and tableb.Field1 = ‘Test%’

this query will result in 3 rows :

1 First 100  
1 First 100  
2 Second 50

which is 250 total…

You dont see that second First because in the normal result set we filter all double pk’s. But we cant do that with a sum.
