# Sub-aggregate on a scripted bucket query?

**URL:** https://discuss.elastic.co/t/sub-aggregate-on-a-scripted-bucket-query/104293
**Category:** Elasticsearch
**Created:** [October 17, 2017, 6:34pm UTC](https://discuss.elastic.co/t/sub-aggregate-on-a-scripted-bucket-query/104293 "2017-10-17T18:34:05Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![casieowen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/casieowen/32/8734_2.png) [@casieowen](https://discuss.elastic.co/u/casieowen)
#### Post date: [October 17, 2017, 6:34pm UTC](https://discuss.elastic.co/t/sub-aggregate-on-a-scripted-bucket-query/104293/1 "2017-10-17T18:34:05Z")

</div>

Hi,

The query below returns a percentage value at a daily grain. What I'm wondering is, can we do a sub aggregation on that to get, say, the min value of all the daily values for a month within this same query/without writing the values to a new index?

GET clientavailability\_\*/\_search  
{  
"size": 1,  
"aggs": {  
"@timestamp": {  
"date\_histogram": {  
"field": "@timestamp",  
"interval": "1d"  
},  
"aggs": {  
"filterSuccess": {  
"filter": {  
"match\_all": {}  
},  
"aggs": {  
"successCount": {  
"sum": {  
"field": "totalSuccessfulRequests"  
}  
}  
}  
},  
"filterTotal": {  
"filter": {  
"match\_all": {}  
},  
"aggs": {  
"totalCount": {  
"sum": {  
"field": "totalRequests"  
}  
}  
}  
},  
"scriptedBucket": {  
"bucket\_script": {  
"buckets\_path": {  
"successCount": "filterSuccess\>successCount",  
"totalCount": "filterTotal\>totalCount"  
},  
"script": {  
"inline": "(params.successCount / params.totalCount) \* 100"  
}  
}  
}  
}  
}  
},  
"query": {  
"bool": {  
"must": [  
{  
"range": {  
"@timestamp": {  
"gte": "2017-07-01T00:00:00Z",  
"lte": "2017-10-31T00:00:00Z"  
}  
}  
},  
{  
"term": {  
"experiences.microExperiences.experience": {  
"value": "Launch Game or App|Launch Game or App"  
}  
}  
}  
]  
}  
}  
}  
Thanks,  
Casie

---

<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 14, 2017, 6:34pm UTC](https://discuss.elastic.co/t/sub-aggregate-on-a-scripted-bucket-query/104293/2 "2017-11-14T18:34:08Z")

</div>

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