# Query DSL: Aggregation of fields and also value of fields

**URL:** <https://discuss.elastic.co/t/query-dsl-aggregation-of-fields-and-also-value-of-fields/285083>\
**Category:** Elasticsearch\
**Created:** [September 24, 2021, 1:30pm UTC](https://discuss.elastic.co/t/query-dsl-aggregation-of-fields-and-also-value-of-fields/285083 "2021-09-24T13:30:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelk](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kelk](https://discuss.elastic.co/u/kelk)\
**Post date:** [September 24, 2021, 1:30pm UTC](https://discuss.elastic.co/t/query-dsl-aggregation-of-fields-and-also-value-of-fields/285083/1 "2021-09-24T13:30:19Z")

</div>

My data structure is as follows

```auto
username,department,salary
bob,finance,2000
joe,HR,2200
chan,finance,3000
bobby,finance,2500
don,HR,1800

```

I was looking for a Splunk's equivalent command of

```auto
... | stats values(salary) by department

```

So basically the output to be `values of the salary` aggregated by `department`

```auto
finance,"2000,3000,2500"
hr,"2200,1800"

```

How to do above aggregation in Watcher/DSL?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 24, 2021, 2:00pm UTC](https://discuss.elastic.co/t/query-dsl-aggregation-of-fields-and-also-value-of-fields/285083/2 "2021-09-24T14:00:58Z")

</div>

It looks like to me a `terms` agg with a `top_hits` sub aggregation.

---

<div class="post-metadata">

**Author:** ![kelk](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kelk](https://discuss.elastic.co/u/kelk)\
**Post date:** [September 24, 2021, 3:11pm UTC](https://discuss.elastic.co/t/query-dsl-aggregation-of-fields-and-also-value-of-fields/285083/3 "2021-09-24T15:11:07Z")

</div>

> [@dadoonet](#):
>
> `top_hits`

thanks for the hint. Let me try and see how it goes

Edit: Above worked. Thanks David

---

<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:** [October 22, 2021, 3:11pm UTC](https://discuss.elastic.co/t/query-dsl-aggregation-of-fields-and-also-value-of-fields/285083/4 "2021-10-22T15:11:27Z")

</div>

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