# Group By Datetime fields While querying

**URL:** https://discuss.elastic.co/t/group-by-datetime-fields-while-querying/339639
**Category:** Elasticsearch
**Created:** [July 31, 2023, 4:03am UTC](https://discuss.elastic.co/t/group-by-datetime-fields-while-querying/339639 "2023-07-31T04:03:12Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![cybercom](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@cybercom](https://discuss.elastic.co/u/cybercom)
#### Post date: [July 31, 2023, 4:03am UTC](https://discuss.elastic.co/t/group-by-datetime-fields-while-querying/339639/1 "2023-07-31T04:03:12Z")

</div>

I need to group by day in my query, so i'm trying to apply group by with datetime field as below:

```json
{
    "_source": "false",
    "query": {
        "match_all": {}
    },
    "aggs": {
        "group_by_weekday": {
            "date_histogram": {
                "field": "record_created_at",
                "calendar_interval": "day",
                "format": "EEEE"
            }
        }
    }
}

```

But i'm getting the results like below?

```json
{
  "aggregations": {
    "group_by_weekday": {
      "buckets": [
        {
          "key_as_string": "Tuesday",
          "key": 1682380800000,
          "doc_count": 13
        },
        {
          "key_as_string": "Wednesday",
          "key": 1682467200000,
          "doc_count": 0
        },
        {
          "key_as_string": "Thursday",
          "key": 1682553600000,
          "doc_count": 1
        },
        {
          "key_as_string": "Wednesday",
          "key": 1683072000000,
          "doc_count": 1
        },
        {
          "key_as_string": "Thursday",
          "key": 1683158400000,
          "doc_count": 1
        }
      ]
    }
  }
}

```

seems like grouping is not being applied here

How can we do this other way to overcome this?

---

<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 28, 2023, 4:04am UTC](https://discuss.elastic.co/t/group-by-datetime-fields-while-querying/339639/2 "2023-08-28T04:04:05Z")

</div>

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