# Get newest value per distinct field

**URL:** <https://discuss.elastic.co/t/get-newest-value-per-distinct-field/241157>\
**Category:** Elasticsearch\
**Created:** [July 14, 2020, 3:35pm UTC](https://discuss.elastic.co/t/get-newest-value-per-distinct-field/241157 "2020-07-14T15:35:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gudmundur](https://avatars.discourse-cdn.com/v4/letter/g/f17d59/32.png) [@gudmundur](https://discuss.elastic.co/u/gudmundur)\
**Post date:** [July 14, 2020, 3:35pm UTC](https://discuss.elastic.co/t/get-newest-value-per-distinct-field/241157/1 "2020-07-14T15:35:15Z")

</div>

Greetings

I want to do a query which returns the newest 1 value per distinct combination of specific underlying fields (maybe also after a certain timestamp)

In the sql world I can do a query like this:

```auto
select Element, Pot, Quantity from Quantities where Id in (
select max(Id) from Quantities group by Element, Pot)
order by Element, Pot
where timestamp > '2020-06-01'

```

The effect of this would be if I had lets say this data:

```auto
Time, Element, Pot, Quantity
2020-06-02, Fe, 1, 100
2020-06-03, Fe, 1, 90
2020-06-03, Fe, 2, 85
2020-06-02, Cu, 1, 100
2020-06-03, Cu, 1, 90

```

I would get something like this back:

```auto
Fe, 1, 90
Fe, 2, 85
Cu, 1, 90

```

How would I do a similar query in ElasticSearch?

Thank you

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [July 14, 2020, 7:59pm UTC](https://discuss.elastic.co/t/get-newest-value-per-distinct-field/241157/2 "2020-07-14T19:59:55Z")

</div>

You can obtain these results using aggregations.  
You can do a [terms aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html) on `Element` field, inside it as a sub-aggregation another terms aggregation on `Pot` field, and inside `Pot` aggregation another sub-aggregation of type [top hits](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html). This `top hits` aggregation can request just a single last document for each distinct `element` and `pot`:

```auto
                    "top_hits": {
                        "sort": [
                            {
                                "time": {
                                    "order": "desc"
                                }
                            }
                        ],
                        "size" : 1
                    }

```

---

<div class="post-metadata">

**Author:** ![gudmundur](https://avatars.discourse-cdn.com/v4/letter/g/f17d59/32.png) [@gudmundur](https://discuss.elastic.co/u/gudmundur)\
**Post date:** [July 15, 2020, 5:15pm UTC](https://discuss.elastic.co/t/get-newest-value-per-distinct-field/241157/3 "2020-07-15T17:15:02Z")

</div>

Thank you Mayya!

For those who might need to solve a similar problem, this is what I ended up with as an equivalent to the sql query above:

```auto
GET index/_search
{
  "size": 0,
  "aggs": {
    "date_ranged": {
      "date_range": {
        "field": "@timestamp",
        "ranges": [
          {
            "from": SOME_DATE_HERE,
            "to": "now"
          }
        ]
      },
      "aggs": {
        "elements": {
          "terms": {
            "field": "Element.keyword"
          },
          "aggs": {
            "pots": {
              "terms": {
                "field": "Pot"
              },
              "aggs": {
                "top_results": {
                  "top_hits": {
                    "sort": [{
                      "@timestamp": {
                        "order": "desc"
                      }
                    }],
                    "size": 1
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

---

<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:** [August 12, 2020, 5:15pm UTC](https://discuss.elastic.co/t/get-newest-value-per-distinct-field/241157/4 "2020-08-12T17:15:06Z")

</div>

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