# Date\_histogram breakdown

**URL:** <https://discuss.elastic.co/t/date-histogram-breakdown/315846>\
**Category:** Elasticsearch\
**Created:** [October 5, 2022, 7:40am UTC](https://discuss.elastic.co/t/date-histogram-breakdown/315846 "2022-10-05T07:40:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aa09](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@aa09](https://discuss.elastic.co/u/aa09)\
**Post date:** [October 5, 2022, 7:40am UTC](https://discuss.elastic.co/t/date-histogram-breakdown/315846/1 "2022-10-05T07:40:37Z")

</div>

I want to be able to break down my data by year and then further by month, im pretty sure this is possible but cant see exactly how.

I am using the following at the moment to get a yearly breakdown:

` 'by_year' => ['date_histogram' => ['field' => 'Date', 'calendar_interval' => 'year', 'format'=>'yyyy']]`

results in

```auto
  0 => array:3 [▼
    "key_as_string" => "2015"
    "key" => 1420070400000
    "doc_count" => 40
  ]

```

this works great but I need it broken down further to include months of that year for example

```auto
 0 => array:3 [▼
   "key_as_string" => "2015"
   "key" => 1420070400000
   "doc_count" => 40,
                [
                   "key_as_string" => "January"
                    "key" => 1420070400000
                    "doc_count" => 4,
               ],
              [
                   "key_as_string" => "February"
                    "key" => 1420070400000
                    "doc_count" => 14,
               ],
              [
                   "key_as_string" => "March"
                    "key" => 1420070400000
                    "doc_count" => 20,
               ],
 ]

```

is this possible?

---

<div class="post-metadata">

**Author:** ![Sourabh\_Korde](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@Sourabh\_Korde](https://discuss.elastic.co/u/Sourabh_Korde)\
**Post date:** [October 5, 2022, 10:02am UTC](https://discuss.elastic.co/t/date-histogram-breakdown/315846/2 "2022-10-05T10:02:48Z")

</div>

@aa09 Please check below post and try to use the script field in your sub aggregation.

> <https://stackoverflow.com/questions/51872420/how-to-group-by-month-in-elastic-search>

for example -

```auto
GET indexname/_search
{
  "size": 0, 
  "aggs": {
    "by_year": {
      "date_histogram": {
        "field": "timestamp",
        "calendar_interval": "year",
        "format": "yyyy"
      },
      "aggs": {
        "month": {
          "terms": {
            "script": "doc['timestamp'].value.getMonthValue()"
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![aa09](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@aa09](https://discuss.elastic.co/u/aa09)\
**Post date:** [October 5, 2022, 10:04am UTC](https://discuss.elastic.co/t/date-histogram-breakdown/315846/3 "2022-10-05T10:04:12Z")

</div>

thanks ill check it out

---

<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:** [November 2, 2022, 10:05am UTC](https://discuss.elastic.co/t/date-histogram-breakdown/315846/4 "2022-11-02T10:05:04Z")

</div>

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