# Count unique customers based on date range

**URL:** <https://discuss.elastic.co/t/count-unique-customers-based-on-date-range/277014>\
**Category:** Elasticsearch\
**Created:** [June 25, 2021, 5:55am UTC](https://discuss.elastic.co/t/count-unique-customers-based-on-date-range/277014 "2021-06-25T05:55:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sanjay\_Agnani1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sanjay_agnani1/32/78176_2.png) [@Sanjay\_Agnani1](https://discuss.elastic.co/u/Sanjay_Agnani1)\
**Post date:** [June 25, 2021, 5:55am UTC](https://discuss.elastic.co/t/count-unique-customers-based-on-date-range/277014/1 "2021-06-25T05:55:53Z")

</div>

Need the stats in unique customer: index contains customers orders data.  
Ex:

```auto
  {
        "_index" : "customers_orders",
        "_type" : "_doc",
        "_id" : "109126",
        "_score" : 1.0,
        "_source" : {
          "total_net" : 64.97,
          "orders_platform" : 2,
          "origin_country" : "DE",
          "currency_iso" : "EUR",
          "internal_id" : 650,
          "ext_platforms_reference" : "T",
          "total_vat" : 12.35,
          "platform" : "eShopingcart",
          "@version" : "1",
          "orders_promised_shipping_date" : "2018-07-14T11:45:35.000Z",
          "orders_shipping_type" : 2,
          "created_at" : "2021-05-09T09:13:25.000Z",
          "@timestamp" : "2021-06-23T03:25:22.448Z",
          "delivery_country" : "DE",
          "orders_comments" : "Special",
          "row_version" : "0x00000000002F25BA",
          "orders_language" : "DE",
          "ext_payments_reference" : null,
          "source_from" : 1,
          "updated_at" : "2021-05-09T09:13:58.000Z",
          "orders_date" : "2018-07-14T11:43:52.000Z",
          "orders_reference" : "VAP-19641-AT",
          "currency_factor" : 1.0,
          "id" : 109126,
          "orders_payment_type" : null,
          "order_status" : 1,
          "total_gross" : 77.32,
          "customer_id" : 47070
        }
      }

```

Need to identify

Blockquote

customers falling in particular range and not orders in betwenn.

So far tried.

```auto
{
  "track_total_hits": true, 
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "orders_date": {
              "gte": "2020-06-26",
              "lte": "2021-06-26"
            }
          }
        }
      ]
     
    }
  
  },
  "aggs": {
    "NAME": {
      "terms": {
        "field": "customer_id",
        "size": 10000
      },
      "aggs": {
        "order_date": {
          "terms": {
            "field": "orders_date",
            "size": 10
          }
        }
        
        
      }
    }
  }
}

```

- How to compare customers multiple orders in aggregation result

---

<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:** [July 23, 2021, 5:56am UTC](https://discuss.elastic.co/t/count-unique-customers-based-on-date-range/277014/2 "2021-07-23T05:56:38Z")

</div>

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