# Pipeline aggregations: apply Histogram over Terms results

**URL:** <https://discuss.elastic.co/t/pipeline-aggregations-apply-histogram-over-terms-results/33767>\
**Category:** Elasticsearch\
**Created:** [November 4, 2015, 4:17pm UTC](https://discuss.elastic.co/t/pipeline-aggregations-apply-histogram-over-terms-results/33767 "2015-11-04T16:17:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkuzmin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mkuzmin/32/5672_2.png) [@mkuzmin](https://discuss.elastic.co/u/mkuzmin)\
**Post date:** [November 4, 2015, 4:17pm UTC](https://discuss.elastic.co/t/pipeline-aggregations-apply-histogram-over-terms-results/33767/1 "2015-11-04T16:17:49Z")

</div>

Hi

I'm trying to analyze user retention metrics based on access logs.  
My index contains documents like these:  
`{ "timestamp": "2015-11-01", "user_id": "1" }`  
`{ "timestamp": "2015-11-02", "user_id": "1" }`  
`{ "timestamp": "2015-11-02", "user_id": "2" }`  
`{ "timestamp": "2015-11-03", "user_id": "2" }`

A query  
``

```

{ "aggs": { "ids": {
"terms": { "field": "user_id" },
"aggs": { "last_date": { "max": { "field": "timestamp", "format": "date" }}}
}}}
```

returns last login date for each user: `{ "buckets": [
    { "key": "1", "last_date": { "value_as_string": "2015-11-02"}},
    { "key": "2", "last_date": { "value_as_string": "2015-11-03"}}
]}
` Now I need to find out how many unique "keys" I have for each day, using date histogram. I've tried referencing a bucket like here `{ 
  "aggs": { 
    "ids": {
      "terms": { "field": "user_id" },
      "aggs": { "last_date": { "max": { "field": "timestamp", "format": "date" }}}
    },  
    "dates": {
      "date_histogram": {
        "buckets_path": "ids>last_date",
        "field": "timestamp",
        "interval": "day",
        "format": "date"
      }
    }
  }
}
` But apparently `date_histogram` cannot accept `buckets_path` parameter.

Is there a way to apply Date histogram over a bucket? Can both these aggregations be calculated in a single query?

---

<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:** [July 5, 2017, 11:40pm UTC](https://discuss.elastic.co/t/pipeline-aggregations-apply-histogram-over-terms-results/33767/2 "2017-07-05T23:40:33Z")

</div>


