# Return date\_histogram in hour for each day

**URL:** https://discuss.elastic.co/t/return-date-histogram-in-hour-for-each-day/128438
**Category:** Elasticsearch
**Created:** [April 18, 2018, 1:44am UTC](https://discuss.elastic.co/t/return-date-histogram-in-hour-for-each-day/128438 "2018-04-18T01:44:04Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ysl](https://avatars.discourse-cdn.com/v4/letter/y/4af34b/32.png) [@ysl](https://discuss.elastic.co/u/ysl)
#### Post date: [April 18, 2018, 1:44am UTC](https://discuss.elastic.co/t/return-date-histogram-in-hour-for-each-day/128438/1 "2018-04-18T01:44:04Z")

</div>

Hello,

I have records with 15m interval timestamp.

Now I want to return with:

timestamps grouped by 60 minutes --\> date\_histogram aggregation with interval at 60m (4 docs in one bucket, 15m per record)  
return the sum of screening\_time for hourly --\> sum aggregation on the screening\_time.  
which hour has the highest screening\_time --\> max bucket (24 buckets a day)

Now I have the max bucket up to one day, it comes to an issue if I have 7 days of data, how do I return the highest value screening\_time for that particular hour each of the day ?

This is my code:

GET test/\_search  
{  
"size": 0,  
"query": {  
"bool": {  
"filter": [  
{ "match": { "category": "subset" } }  
]  
}  
},  
"aggs" : {  
"screening\_time" : {  
"date\_histogram" : {  
"field" : "timestamp",  
"interval" : "60m"  
},  
"aggs": {  
"sales": {  
"sum": {  
"field": "total\_sales"  
}  
}  
}  
},  
"max\_monthly\_sales": {  
"max\_bucket": {  
"buckets\_path": "screening\_time\>sales"  
}  
}  
}  
}

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [April 18, 2018, 4:27am UTC](https://discuss.elastic.co/t/return-date-histogram-in-hour-for-each-day/128438/2 "2018-04-18T04:27:28Z")

</div>

Not sure but it'd be probably easier if you index that value (hour of the day) in a field at index time.

Otherwise you can probably use [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html) to compute the hour of the day but I guess it will be slower to execute.

---

<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: [May 16, 2018, 4:27am UTC](https://discuss.elastic.co/t/return-date-histogram-in-hour-for-each-day/128438/3 "2018-05-16T04:27:42Z")

</div>

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