# Aggregation: unique count grouped by combination of field value

**URL:** <https://discuss.elastic.co/t/aggregation-unique-count-grouped-by-combination-of-field-value/287431>\
**Category:** Kibana\
**Created:** [October 22, 2021, 11:29am UTC](https://discuss.elastic.co/t/aggregation-unique-count-grouped-by-combination-of-field-value/287431 "2021-10-22T11:29:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ismarslomic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ismarslomic/32/63870_2.png) [@ismarslomic](https://discuss.elastic.co/u/ismarslomic)\
**Post date:** [October 22, 2021, 11:29am UTC](https://discuss.elastic.co/t/aggregation-unique-count-grouped-by-combination-of-field-value/287431/1 "2021-10-22T11:29:35Z")

</div>

Given following documents (simplified) in an index:

```auto
{
  "vehicle": "A",
  "source": "foo"
}
{
  "vehicle": "A",
  "source": "bar"
}
{
  "vehicle": "A",
  "source": "foo"
}
{
  "vehicle": "B",
  "source": "foo"
}
{
  "vehicle": "B",
  "source": "foo"
}
{
  "vehicle": "C",
  "source": "bar"
}

```

How can I create aggregation to retrieve unique count of vehicles grouped by source combinations with following result:

```auto
Source Unique vehicle count
====================================
foo 1 (this is vehicle B)
bar 1 (this is vehicle C)
foo, bar 1 (this is vehicle A)

```

---

<div class="post-metadata">

**Author:** ![ismarslomic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ismarslomic/32/63870_2.png) [@ismarslomic](https://discuss.elastic.co/u/ismarslomic)\
**Post date:** [October 26, 2021, 7:54am UTC](https://discuss.elastic.co/t/aggregation-unique-count-grouped-by-combination-of-field-value/287431/2 "2021-10-26T07:54:48Z")

</div>

Any input on this would be highly appreciated.

If it is not possible to achieve such query with current document fields we could adjust the document content to make it easier.

---

<div class="post-metadata">

**Author:** ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)\
**Post date:** [October 27, 2021, 4:30pm UTC](https://discuss.elastic.co/t/aggregation-unique-count-grouped-by-combination-of-field-value/287431/3 "2021-10-27T16:30:32Z")

</div>

With the way the data is currently indexed, I don't think you can accomplish exactly what you'd like; the closest you could get is something like this:

```auto
Source Unique vehicle count
====================================
foo 2 (this is vehicle A/B)
bar 1 (this is vehicle C)

```

You'd have to use a separate index where you actually index the vehicle with the different `source` combinations.

---

<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:** [November 24, 2021, 4:30pm UTC](https://discuss.elastic.co/t/aggregation-unique-count-grouped-by-combination-of-field-value/287431/4 "2021-11-24T16:30:46Z")

</div>

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