# Group by date with aggr and apply sub aggr with group by date in elastic search

**URL:** <https://discuss.elastic.co/t/group-by-date-with-aggr-and-apply-sub-aggr-with-group-by-date-in-elastic-search/309894>\
**Category:** Elasticsearch\
**Created:** [July 18, 2022, 11:49am UTC](https://discuss.elastic.co/t/group-by-date-with-aggr-and-apply-sub-aggr-with-group-by-date-in-elastic-search/309894 "2022-07-18T11:49:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![surendrakandira](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/surendrakandira/32/108447_2.png) [@surendrakandira](https://discuss.elastic.co/u/surendrakandira)\
**Post date:** [July 18, 2022, 11:49am UTC](https://discuss.elastic.co/t/group-by-date-with-aggr-and-apply-sub-aggr-with-group-by-date-in-elastic-search/309894/1 "2022-07-18T11:49:37Z")

</div>

How to implement below C# entity logic in Elasticsearch

```auto
var queryable1 = queryable.GroupBy(g => g.Date).Select(x => new GroupChartDto() 
{
 DailyPnL = x.Sum(s => s.DailyPnL), 
Date = x.Key, 
AggregateSum = queryable.Where(w => w.Date.Date <= x.Key.Date).Sum(s => s.DailyPnL) }).ToList();

```

I have applied query

`{ "query":{ "bool": { "filter": [{"terms": {"account": ["abc","xyz","rs876909kk"]}}, {"range": {"date": {"gte": "2021-07-12","lt": "2022-07-12"}}} ] } }, "aggs": { "group_by_month": { "date_histogram": { "field": "date", "calendar_interval": "month", "format": "yyyy-MM-dd" }, "sum_dailyPnL":{ "filter": {"range": {"date": {"lt": "group_by_month.key_as_string"}}}, "aggs": {"sum_dailyPnL": { "sum": { "field": "dailyPnL" } }} } } } }`

---

<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:** [August 15, 2022, 11:49am UTC](https://discuss.elastic.co/t/group-by-date-with-aggr-and-apply-sub-aggr-with-group-by-date-in-elastic-search/309894/2 "2022-08-15T11:49:41Z")

</div>

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