# Sql query, histogram interval one month problem

**URL:** <https://discuss.elastic.co/t/sql-query-histogram-interval-one-month-problem/216442>\
**Category:** Elasticsearch\
**Created:** [January 24, 2020, 2:36pm UTC](https://discuss.elastic.co/t/sql-query-histogram-interval-one-month-problem/216442 "2020-01-24T14:36:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [January 24, 2020, 2:36pm UTC](https://discuss.elastic.co/t/sql-query-histogram-interval-one-month-problem/216442/1 "2020-01-24T14:36:48Z")

</div>

I am trying to do histogram in sql query for month and it does not work

daily or hourly works fine here is query

```
GET _sql?format=txt
{
  "query": """
        select histogram(achieved, INTERVAL 1 month) as m, count(*) as c from "resource-*" where  
      achieved between '2019-01-01' and '2019-12-31' 
        group by m
        """
}  

```

Output comes out with middle of the month. and first one is even for 2018. Where am I wrong?

2018-12-15T00:00:00.000Z|2959402  
2019-01-14T00:00:00.000Z|7444959  
2019-02-13T00:00:00.000Z|8592928

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [January 24, 2020, 6:13pm UTC](https://discuss.elastic.co/t/sql-query-histogram-interval-one-month-problem/216442/2 "2020-01-24T18:13:26Z")

</div>

FYI using 7.5.0

---

<div class="post-metadata">

**Author:** ![Andrei\_Stefan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei_stefan/32/47533_2.png) [@Andrei\_Stefan](https://discuss.elastic.co/u/Andrei_Stefan)\
**Post date:** [January 25, 2020, 1:50pm UTC](https://discuss.elastic.co/t/sql-query-histogram-interval-one-month-problem/216442/3 "2020-01-25T13:50:18Z")

</div>

@elasticforme can you post the ES query dsl for that query, please?  
Use `translate` API for this. [https://www.elastic.co/guide/en/elasticsearch/reference/7.x/sql-translate.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/sql-translate.html)

I think I know why you get those results, but showing the actual query could demonstrate my point.

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [January 27, 2020, 7:35pm UTC](https://discuss.elastic.co/t/sql-query-histogram-interval-one-month-problem/216442/4 "2020-01-27T19:35:05Z")

</div>

Here it is

```
{
  "size" : 0,
  "query" : {
    "range" : {
      "achieved" : {
        "from" : "2019-01-01",
        "to" : "2019-12-31",
        "include_lower" : true,
        "include_upper" : true,
        "boost" : 1.0
      }
    }
  },
  "_source" : false,
  "stored_fields" : "_none_",
  "aggregations" : {
    "groupby" : {
      "composite" : {
        "size" : 1000,
        "sources" : [
          {
            "3536" : {
              "date_histogram" : {
                "field" : "achieved",
                "missing_bucket" : true,
                "value_type" : "date",
                "order" : "asc",
                "fixed_interval" : "2592000000ms",
                "time_zone" : "Z"
              }
            }
          }
        ]
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![Andrei\_Stefan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei_stefan/32/47533_2.png) [@Andrei\_Stefan](https://discuss.elastic.co/u/Andrei_Stefan)\
**Post date:** [January 28, 2020, 10:46am UTC](https://discuss.elastic.co/t/sql-query-histogram-interval-one-month-problem/216442/5 "2020-01-28T10:46:51Z")

</div>

Thank you @elasticforme.  
The relevant part in the query you provided is `"fixed_interval" : "2592000000ms"` which represent the length of a month (30 days) in milliseconds.

Elasticsearch has two ways in which it can represent an interval in a `date_histogram`: [a calendar interval and a fixed interval](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html#calendar_and_fixed_intervals). There is a detailed explanation on the differences between the two in [this section from the documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html#calendar_and_fixed_intervals).

In SQL we chose to represent these as fixed intervals, and the behavior you get is the result of this choice. We did make a [change for YEAR](https://github.com/elastic/elasticsearch/pull/47558) to behave like a calendar interval and we could to the same for MONTH and maybe DAY.

Can you, please, open an issue in [https://github.com/elastic/elasticsearch/](https://github.com/elastic/elasticsearch/) about an improvement to `GROUP BY HISTOGRAM` where the interval is `1 MONTH` or `1 DAY`? Thanks.

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [January 28, 2020, 2:58pm UTC](https://discuss.elastic.co/t/sql-query-histogram-interval-one-month-problem/216442/6 "2020-01-28T14:58:04Z")

</div>

ok open the issue. didn't know how to do it. as never use git for anything.

[https://github.com/elastic/elasticsearch/issues/51538](https://github.com/elastic/elasticsearch/issues/51538)

---

<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:** [February 25, 2020, 2:58pm UTC](https://discuss.elastic.co/t/sql-query-histogram-interval-one-month-problem/216442/7 "2020-02-25T14:58:05Z")

</div>

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