# Pivot Transform Count of documents

**URL:** <https://discuss.elastic.co/t/pivot-transform-count-of-documents/300082>\
**Category:** Elasticsearch\
**Tags:** transforms\
**Created:** [March 18, 2022, 6:59pm UTC](https://discuss.elastic.co/t/pivot-transform-count-of-documents/300082 "2022-03-18T18:59:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamtheschmitzer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iamtheschmitzer/32/96988_2.png) [@iamtheschmitzer](https://discuss.elastic.co/u/iamtheschmitzer)\
**Post date:** [March 18, 2022, 6:59pm UTC](https://discuss.elastic.co/t/pivot-transform-count-of-documents/300082/1 "2022-03-18T18:59:35Z")

</div>

This seems to work, is it the best way?

In a pivot transform, I want a count of the documents included in the aggregation. I tried this and it seemed to work. However feeling uneasy without a vote of confidence from the community.

I tried this aggregation:

```auto
  "pivot": {
    "group_by": {
      // omitted
    },
    "aggregations": {
      "job_count": {
        "value_count": {
          "field": "_id"
        }
      }
    }
  }

```

---

<div class="post-metadata">

**Author:** ![BenTrent](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bentrent/32/33915_2.png) [@BenTrent](https://discuss.elastic.co/u/BenTrent)\
**Post date:** [March 18, 2022, 7:14pm UTC](https://discuss.elastic.co/t/pivot-transform-count-of-documents/300082/2 "2022-03-18T19:14:20Z")

</div>

```auto
    "group_by": {
      // omitted
    },
    "aggregations": {
      "job_count": {
        "value_count": {
          "field": "_id"
        }
      }
    }
  }

```

That works just fine. Another option would be to use a field from your `group_by`. It is required that all those fields exist in the resulting set anyways.

I am not sure it will be any faster or slower.

---

<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:** [April 15, 2022, 7:14pm UTC](https://discuss.elastic.co/t/pivot-transform-count-of-documents/300082/3 "2022-04-15T19:14:58Z")

</div>

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