# Pivoting in Elasticsearch

**URL:** <https://discuss.elastic.co/t/pivoting-in-elasticsearch/24555>\
**Category:** Kibana\
**Created:** [June 29, 2015, 3:11pm UTC](https://discuss.elastic.co/t/pivoting-in-elasticsearch/24555 "2015-06-29T15:11:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![depp1993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/depp1993/32/3526_2.png) [@depp1993](https://discuss.elastic.co/u/depp1993)\
**Post date:** [June 29, 2015, 3:11pm UTC](https://discuss.elastic.co/t/pivoting-in-elasticsearch/24555/1 "2015-06-29T15:11:38Z")

</div>

Hey ,

Can anybody tell me is there a way in elasticsearch/kibana to provide pivoting feature as in Oracle?

I want to create a report with pivoting a field with sum as aggregation.

For example : I have this schema indexed in `test-index/test-type` Doc id being `ID`.

```
ID | NAME | Code | AMT
-----------------------
1 | ABC | a | 10
2 | DEF | c | 20
3 | XYZ | a | 30
4 | ABC | a | 10
5 | ABC | c | 20
6 | XYZ | b | 30
7 | DEF | c | 10
8 | DEF | a | 20

```

I want to generate a report pivoting done like this : SUM(AMT) FOR CODE IN (a,b,c)

```
NAME | a | b | c 
---------------------
ABC | 20 | 0 | 20
DEF | 20 | 0 | 30
XYZ | 30 | 30 | 0

```

Can we do it using elasticsearch and reports generated in kibana?  
PS. I am new to Elasticsearch Query DSL, so it would be helpful if someone having good grasp on query DSL can help me out whether its possible in ES , or suggest me a way to do this??

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [June 30, 2015, 5:30am UTC](https://discuss.elastic.co/t/pivoting-in-elasticsearch/24555/2 "2015-06-30T05:30:44Z")

</div>

You can do a table with an agg on `NAME` and then a sub-agg using a sum, on `Code`.  
That'll look like this;

 ![](https://us1.discourse-cdn.com/elastic/original/2X/6/62259eb6e06cf642c802ca10a2ba668bcf191dcc.jpg)

Not sure you can have it in the format you are after though.

---

<div class="post-metadata">

**Author:** ![depp1993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/depp1993/32/3526_2.png) [@depp1993](https://discuss.elastic.co/u/depp1993)\
**Post date:** [June 30, 2015, 7:03am UTC](https://discuss.elastic.co/t/pivoting-in-elasticsearch/24555/3 "2015-06-30T07:03:55Z")

</div>

Thanks for your reply @warkolm . But can you tell me how to Pivot?? i.e. make field value as a field and do aggregation upon it? Any solution to it ?

---

<div class="post-metadata">

**Author:** ![tbragin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tbragin/32/45166_2.png) [@tbragin](https://discuss.elastic.co/u/tbragin)\
**Post date:** [July 1, 2015, 4:42am UTC](https://discuss.elastic.co/t/pivoting-in-elasticsearch/24555/4 "2015-07-01T04:42:31Z")

</div>

I don't think there is a direct equivalent to a pivot. I would also propose doing a "terms" aggregation on the "NAME" field and then adding the rest of the fields as columns in the table, as @warkolm showed in the screenshot.

---

<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 6, 2017, 2:17pm UTC](https://discuss.elastic.co/t/pivoting-in-elasticsearch/24555/5 "2017-07-06T14:17:15Z")

</div>


