# Metrics from Aggregation results across two different buckets

**URL:** <https://discuss.elastic.co/t/metrics-from-aggregation-results-across-two-different-buckets/177510>\
**Category:** Elasticsearch\
**Created:** [April 18, 2019, 4:24pm UTC](https://discuss.elastic.co/t/metrics-from-aggregation-results-across-two-different-buckets/177510 "2019-04-18T16:24:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ankit\_Pradhan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankit_pradhan/32/44412_2.png) [@Ankit\_Pradhan](https://discuss.elastic.co/u/Ankit_Pradhan)\
**Post date:** [April 18, 2019, 4:24pm UTC](https://discuss.elastic.co/t/metrics-from-aggregation-results-across-two-different-buckets/177510/1 "2019-04-18T16:24:39Z")

</div>

Plesse help

Index has data like below

{ID:ID1, Status:Status1, timeStamp:Timestamp1}  
{ID1 - Status2 - Timestamp 2}  
{ID1 - Status3 - Timestamp 3}

{ID2 - Status 1 - Timestamp 2}  
{ID2 - Status 2 - Timestamp 3}

{ID3 - Status 2 - Timestamp 2}  
{ID3 - Status 3 - Timestamp 3}

{ID4 - Status 1 - Timestamp 1}

When I search after Timestamp 3, I would like to get the count of IDs per Latest Status for that ID  
e.g.  
Status3 - Count 2 IDs (because of ID1 & ID3)  
Status2 - Count 1 ID (because of ID2)  
Status1 - Count 1 ID (because of ID4)

---

<div class="post-metadata">

**Author:** ![cfeenstra67](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cfeenstra67/32/43712_2.png) [@cfeenstra67](https://discuss.elastic.co/u/cfeenstra67)\
**Post date:** [April 18, 2019, 6:24pm UTC](https://discuss.elastic.co/t/metrics-from-aggregation-results-across-two-different-buckets/177510/2 "2019-04-18T18:24:04Z")

</div>

I hope I'm understanding you correctly, but this seems pretty straightforward actually. If you are just going to be querying for 1 TS at once

```auto
GET <index>/_search
{
  "size": 0,
  "query": {
    "term": {
      "timestamp": <Timestamp3>
    }
  },
  "aggs": {
    "statuses": {
      "terms": {
        "field": "status"
        // You may want to add additional params to the terms
        // aggregation here to set the # of buckets or ordering for example
      },
      "aggs": {
        "ids": {
          "cardinality": {"field": "ID"}
        }
      }
    }
  }
}

```

You can also do it w/ multiple timestamps at once if you want--you could use either two `terms` aggregations or a `composite` aggregation

---

<div class="post-metadata">

**Author:** ![Ankit\_Pradhan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankit_pradhan/32/44412_2.png) [@Ankit\_Pradhan](https://discuss.elastic.co/u/Ankit_Pradhan)\
**Post date:** [April 19, 2019, 9:03am UTC](https://discuss.elastic.co/t/metrics-from-aggregation-results-across-two-different-buckets/177510/3 "2019-04-19T09:03:32Z")

</div>

Thanks for your revert ...  
the solution which you propose does give the status count back ...  
But  
What I would like to have is the count of Top status (derived by Sorting on timestamp: desc)

At the moment it is giving status count per field ID but i would like to get a total count of status depending on whether the status is latest status for that ID group

Please see if you can help think further

---

<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:** [May 17, 2019, 9:03am UTC](https://discuss.elastic.co/t/metrics-from-aggregation-results-across-two-different-buckets/177510/4 "2019-05-17T09:03:38Z")

</div>

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