# Aggregate 7 fields and get the sum of another field

**URL:** <https://discuss.elastic.co/t/aggregate-7-fields-and-get-the-sum-of-another-field/261211>\
**Category:** Elasticsearch\
**Created:** [January 15, 2021, 6:00am UTC](https://discuss.elastic.co/t/aggregate-7-fields-and-get-the-sum-of-another-field/261211 "2021-01-15T06:00:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vands](https://avatars.discourse-cdn.com/v4/letter/v/cc9497/32.png) [@vands](https://discuss.elastic.co/u/vands)\
**Post date:** [January 15, 2021, 6:00am UTC](https://discuss.elastic.co/t/aggregate-7-fields-and-get-the-sum-of-another-field/261211/1 "2021-01-15T06:00:16Z")

</div>

Hi All,

I have a scenario , where I would need to aggregate 7 fields and get the sum of other field with each unique set . Any suggestions which would solve my issue.

---

<div class="post-metadata">

**Author:** ![Hendrik\_Muhs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrik_muhs/32/25802_2.png) [@Hendrik\_Muhs](https://discuss.elastic.co/u/Hendrik_Muhs)\
**Post date:** [January 15, 2021, 7:26am UTC](https://discuss.elastic.co/t/aggregate-7-fields-and-get-the-sum-of-another-field/261211/2 "2021-01-15T07:26:11Z")

</div>

Hi,

it depends, can you be a bit more specific?

> aggregate 7 fields

7 _different_ fields? What aggregation? Sum? If it's _different_ fields, I think you need a `scripted_metric` as aggs normally work on 1 field, not a list of fields.

> with each unique set

Do you want this for _all_ your data or just for a specific subset? If you want it only for a specific id or a some id's you can do this as part of a search request.

If you want to aggregate the whole data set you can use a _composite aggregation_, this enables you to page trough the results. This is good if you want to directly process the output.

If you want to store the results in an index, have a look at _transform_, which is basically a wrapper around composite aggregation that writes the output to a new index.

---

<div class="post-metadata">

**Author:** ![vands](https://avatars.discourse-cdn.com/v4/letter/v/cc9497/32.png) [@vands](https://discuss.elastic.co/u/vands)\
**Post date:** [January 15, 2021, 7:41am UTC](https://discuss.elastic.co/t/aggregate-7-fields-and-get-the-sum-of-another-field/261211/3 "2021-01-15T07:41:09Z")

</div>

Hi Hendrik,

Thanks for you reply.

I would need to get unique values from 7 different fields and get per day cost [aggregation] of this result set .

This would need to be executed on whole data in the index not on a subset .

Thanks In Advance.

---

<div class="post-metadata">

**Author:** ![Hendrik\_Muhs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrik_muhs/32/25802_2.png) [@Hendrik\_Muhs](https://discuss.elastic.co/u/Hendrik_Muhs)\
**Post date:** [January 15, 2021, 7:48am UTC](https://discuss.elastic.co/t/aggregate-7-fields-and-get-the-sum-of-another-field/261211/4 "2021-01-15T07:48:10Z")

</div>

Does unique values mean the _number of_ different values, aka _cardinality_? -\> cardinality aggregation

Or do you need the exact values? -\> terms aggregation

As said, have a look at composite aggregation. However, as you mention "per day", I assume you want to run the repeatedly. If that's the case, I think [transform](https://www.elastic.co/guide/en/elasticsearch/reference/current/transforms.html) might be the right tool. It ships with a UI in kibana which is a great helper to setup a transform.

---

<div class="post-metadata">

**Author:** ![vands](https://avatars.discourse-cdn.com/v4/letter/v/cc9497/32.png) [@vands](https://discuss.elastic.co/u/vands)\
**Post date:** [January 15, 2021, 10:14am UTC](https://discuss.elastic.co/t/aggregate-7-fields-and-get-the-sum-of-another-field/261211/5 "2021-01-15T10:14:25Z")

</div>

Thanks for your help Hendrick

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 12, 2021, 10:14am UTC](https://discuss.elastic.co/t/aggregate-7-fields-and-get-the-sum-of-another-field/261211/6 "2021-02-12T10:14:30Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
