# Need some help creating a complex query/aggregation

**URL:** <https://discuss.elastic.co/t/need-some-help-creating-a-complex-query-aggregation/242402>\
**Category:** Elasticsearch\
**Created:** [July 23, 2020, 8:51pm UTC](https://discuss.elastic.co/t/need-some-help-creating-a-complex-query-aggregation/242402 "2020-07-23T20:51:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sKurryBoii](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/skurryboii/32/45746_2.png) [@sKurryBoii](https://discuss.elastic.co/u/sKurryBoii)\
**Post date:** [July 23, 2020, 8:51pm UTC](https://discuss.elastic.co/t/need-some-help-creating-a-complex-query-aggregation/242402/1 "2020-07-23T20:51:04Z")

</div>

I'm trying to create a query to help create a retention cohort.  
Essentially, my index consists of many records with the fields username,timestamp,taskCompleted  
From this data I'm trying to group users into weeks (based on their timestamp) where taskCompleted=registration (essentially grouping users by registration week), and for all users that registered in a given week, I want to bucket all their activity by weeks.

For example, if I have 100 users total, 10 of which registered on week X. For those 10 users, I want to bucket all their activity into a week by week basis, which I then plan to apply cardinality on. I then want this to happen not just for registrations on week X, but weeks X-Z.

If this is still confusing, a retention cohort is splitting users by week of registration, and for every time period (which is a week), how many users had an activity processed.

---

<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 20, 2020, 8:51pm UTC](https://discuss.elastic.co/t/need-some-help-creating-a-complex-query-aggregation/242402/2 "2020-08-20T20:51:12Z")

</div>

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