# Having trouble creating aggregation

**URL:** <https://discuss.elastic.co/t/having-trouble-creating-aggregation/189421>\
**Category:** Elasticsearch\
**Created:** [July 8, 2019, 7:53pm UTC](https://discuss.elastic.co/t/having-trouble-creating-aggregation/189421 "2019-07-08T19:53:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgelb](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@rgelb](https://discuss.elastic.co/u/rgelb)\
**Post date:** [July 8, 2019, 7:53pm UTC](https://discuss.elastic.co/t/having-trouble-creating-aggregation/189421/1 "2019-07-08T19:53:20Z")

</div>

I have an index with following fields, among others:

- yearBuilt (a number like 1975 that indicates the year a house was constructed)
- zipCode

I want to create an aggregation which shows the average YearBuilt by each ZipCode. I got the following, but not sure how to filter out documents that don't have the ZipCode or YearBuilt columns?

```
POST /properties/_search
{
  "size": 0, 
  "aggs": {
    "zips": {
      "terms": {
        "field": "zipCode",
        "size": 100
      },
      "aggs": {
        "avgYearBuilt": {
          "avg": {
            "field": "yearBuilt"
          }
        }
      }
    }
  }
}
```

---

<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 5, 2019, 7:53pm UTC](https://discuss.elastic.co/t/having-trouble-creating-aggregation/189421/2 "2019-08-05T19:53:22Z")

</div>

Closed on August 5, 2019, 7:53pm UTC

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