# Query to get latest log entries grouped by a field

**URL:** <https://discuss.elastic.co/t/query-to-get-latest-log-entries-grouped-by-a-field/55748>\
**Category:** Elasticsearch\
**Created:** [July 18, 2016, 11:00am UTC](https://discuss.elastic.co/t/query-to-get-latest-log-entries-grouped-by-a-field/55748 "2016-07-18T11:00:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sruthi](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@sruthi](https://discuss.elastic.co/u/sruthi)\
**Post date:** [July 18, 2016, 11:00am UTC](https://discuss.elastic.co/t/query-to-get-latest-log-entries-grouped-by-a-field/55748/1 "2016-07-18T11:00:35Z")

</div>

Suppose you have logs with KEYWORDS [100, 101, 102] with same ID='new\_id'.  
Need to get the latest inserted record with all fields based on timestamp (say, 102 based on timestamp).  
Equivalent to SQL: SELECT TOP 1, \* FROM [TABLE\_NAME] GROUP BY ID ORDER BY timestamp DESC

---

<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, 10:34pm UTC](https://discuss.elastic.co/t/query-to-get-latest-log-entries-grouped-by-a-field/55748/2 "2017-07-05T22:34:42Z")

</div>


