# Elasticsearch group by, having and pagenation

**URL:** <https://discuss.elastic.co/t/elasticsearch-group-by-having-and-pagenation/248251>\
**Category:** Elasticsearch\
**Created:** [September 11, 2020, 2:24am UTC](https://discuss.elastic.co/t/elasticsearch-group-by-having-and-pagenation/248251 "2020-09-11T02:24:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![coolyrat](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/coolyrat/32/75406_2.png) [@coolyrat](https://discuss.elastic.co/u/coolyrat)\
**Post date:** [September 11, 2020, 2:24am UTC](https://discuss.elastic.co/t/elasticsearch-group-by-having-and-pagenation/248251/1 "2020-09-11T02:24:42Z")

</div>

I have a SQL that would like to query the same result set in ES. The result set would need to pagination. So I've tried [Composite aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html), it works fine for pagination and order. But I have no idea how to filter with the `Having Statement` .

```sql
SELECT student_id,COUNT(paid_amount) paid_total 
  FROM paid_records 
  GROUP BY student_id 
    HAVING paid_total > 100 
  ORDER BY paid_total desc OFFSET 20 LIMIT 10;

```

Aggs Approach, this approach can't pagination:

```auto
{
  "size": 0,
  "aggs": {
    "groupby_student": {
      "terms": {
        "field": "student_id"
      },
      "aggs": {
        "paid_price_sum": {
          "sum": {
            "field": "total_paid_price"
          }
        },
        "paid_price_sum_filter": {
          "bucket_selector": {
            "buckets_path": {
              "priceSum": "paid_price_sum"
            },
            "script": "params.priceSum >= 100"
          }
        },
        "paid_price_sort": {
          "bucket_sort": {
            "sort": {
              "paid_price_sum": "desc"
            },
            "size": 10
          }
        }
      }
    }
  }
}

```

Composite Agg approach, the result set is not correct, with the `paid_price_sum_filter` it just filters out the current page.

```auto
{
  "size": 0,
  "aggs": {
    "groupBy": {
      "composite": {
        "size": 10,
        "after": {"paid_count": "5c8a1e4eede4790001b1d74b"},
        "sources": [
          {
            "paid_count": {
              "terms": {
                "field": "student_id"
              }
            }
          }
        ]
      },
      "aggs": {
        "paid_price_sum": {
          "sum": {
            "field": "total_paid_price"
          }
        },
        "paid_price_sum_filter": {
          "bucket_selector": {
            "buckets_path": {
              "paid_price_sum": "paid_price_sum"
            },
            "script": "params.paid_price_sum > 100"
          }
        }
      }
    }
  }
}

```

---

<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:** [October 9, 2020, 2:24am UTC](https://discuss.elastic.co/t/elasticsearch-group-by-having-and-pagenation/248251/2 "2020-10-09T02:24:47Z")

</div>

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