# Histogram aggregation over unique IDs: possible with nested aggregations?

**URL:** <https://discuss.elastic.co/t/histogram-aggregation-over-unique-ids-possible-with-nested-aggregations/250539>\
**Category:** Elasticsearch\
**Created:** [September 30, 2020, 4:25pm UTC](https://discuss.elastic.co/t/histogram-aggregation-over-unique-ids-possible-with-nested-aggregations/250539 "2020-09-30T16:25:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandbender](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sandbender/32/66398_2.png) [@sandbender](https://discuss.elastic.co/u/sandbender)\
**Post date:** [September 30, 2020, 4:25pm UTC](https://discuss.elastic.co/t/histogram-aggregation-over-unique-ids-possible-with-nested-aggregations/250539/1 "2020-09-30T16:25:50Z")

</div>

Hello,

I am trying to aggregate data for a heatmap visualisation in Vega-Lite. It is supposed to show the volume of communication (color) in volume ranges (y-axis) over time (x-axis).

Where I'm stuck at right now is: that I need to get a unique count of message IDs (since message events are spread across multiple documents) **before** aggregating them into a histogram. This I thought I could achieve by a cardinality agg which then feeds into a histogram agg. But that's not possible since there is no pipeline agg of the histogram flavour.

Would anyone have an idea how to achieve this?

The following code doesn't work but I hope it demonstrates what I'm trying to do:

```auto
{
  "aggs": {
    "msg_hour_of_week": {
      "histogram": {
        "field": "time.hourOfWeek",
        "interval": 1,
        "min_doc_count": 0
      },
      "aggs": {
        "unique_count": {
          "cardinality": {
            "field": "messageId"
          }
        },
        "msg_vol_histo": {
          "histogram": {
            "buckets_path": "unique_count",
            "interval": 100
          }
        }
      }
    }
  },
  "size": 0
}

```

---

<div class="post-metadata">

**Author:** ![sandbender](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sandbender/32/66398_2.png) [@sandbender](https://discuss.elastic.co/u/sandbender)\
**Post date:** [October 8, 2020, 6:07pm UTC](https://discuss.elastic.co/t/histogram-aggregation-over-unique-ids-possible-with-nested-aggregations/250539/2 "2020-10-08T18:07:55Z")

</div>

In case anyone finds this in the archives: Using transforms seems the way to go, at least in my case where I want to automatically generate reports once a week or day.

---

<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 5, 2020, 6:08pm UTC](https://discuss.elastic.co/t/histogram-aggregation-over-unique-ids-possible-with-nested-aggregations/250539/3 "2020-11-05T18:08:05Z")

</div>

Closed on November 5, 2020, 6:08pm UTC

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