# Use Date Histogram to group data by day of the week

**URL:** https://discuss.elastic.co/t/use-date-histogram-to-group-data-by-day-of-the-week/295255
**Category:** Elasticsearch
**Created:** [January 24, 2022, 4:47pm UTC](https://discuss.elastic.co/t/use-date-histogram-to-group-data-by-day-of-the-week/295255 "2022-01-24T16:47:33Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![TomTom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomtom/32/3676_2.png) [@TomTom](https://discuss.elastic.co/u/TomTom)
#### Post date: [January 24, 2022, 4:47pm UTC](https://discuss.elastic.co/t/use-date-histogram-to-group-data-by-day-of-the-week/295255/1 "2022-01-24T16:47:33Z")

</div>

My case is as follows, I am conducting a search for sales that occurred between 2021-12-23 to 2022-01-23, and I need to know the total sales by day of the week, regardless of the week and year.  
So I can theoretically know which day of the week has the most sales.

For this I used the Date Histogram, however, it does not group the results by day of the week, bringing the day of the week and the total sales in a formatted way.  
How do I gather all sales per day, regardless of the week?

Code:

```auto
{
    "_source": false,
    "stored_fields": "_none_",
    "aggs": {
        "sales_over_dayofweek": {
            "date_histogram": {
                "field": "date",
                "format": "e",
                "interval": "day"
            }
        }
    }
}

```

Result:

```auto
"aggregations": {
        "sales_over_dayofweek": {
            "buckets": [
                {
                    "key_as_string": "5",
                    "key": 1642118400000,
                    "doc_count": 1
                },
                {
                    "key_as_string": "6",
                    "key": 1642204800000,
                    "doc_count": 0
                },
                {
                    "key_as_string": "7",
                    "key": 1642291200000,
                    "doc_count": 0
                },
                {
                    "key_as_string": "1",
                    "key": 1642377600000,
                    "doc_count": 0
                },
                {
                    "key_as_string": "2",
                    "key": 1642464000000,
                    "doc_count": 0
                },
                {
                    "key_as_string": "3",
                    "key": 1642550400000,
                    "doc_count": 1
                },
                {
                    "key_as_string": "4",
                    "key": 1642636800000,
                    "doc_count": 0
                },
                {
                    "key_as_string": "5",
                    "key": 1642723200000,
                    "doc_count": 3
                }
            ]
        }
    }

```

---

<div class="post-metadata">

### Author: ![TomTom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomtom/32/3676_2.png) [@TomTom](https://discuss.elastic.co/u/TomTom)
#### Post date: [January 24, 2022, 6:09pm UTC](https://discuss.elastic.co/t/use-date-histogram-to-group-data-by-day-of-the-week/295255/2 "2022-01-24T18:09:36Z")

</div>

**Problem Solved With:**

```auto
{
    "_source": false,
    "stored_fields": "_none_",
    "aggregations": {
        "timeslice": {
            "histogram": {
                "script": "doc['date'].value.getDayOfWeek()",
                "interval": 1,
                "min_doc_count": 0,
                "extended_bounds": {
                    "min": 1,
                    "max": 7
                },
                "order": {
                    "_key": "desc"
                }
            }
        }
    }
}

```

Based on: [Elasticsearch Aggregation by Day of Week and Hour of Day - Stack Overflow](https://stackoverflow.com/questions/28989336/elasticsearch-aggregation-by-day-of-week-and-hour-of-day)

---

<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 21, 2022, 6:10pm UTC](https://discuss.elastic.co/t/use-date-histogram-to-group-data-by-day-of-the-week/295255/3 "2022-02-21T18:10:28Z")

</div>

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