# How to build my Elasticsearch query

**URL:** <https://discuss.elastic.co/t/how-to-build-my-elasticsearch-query/135142>\
**Category:** Elasticsearch\
**Created:** [June 8, 2018, 11:37am UTC](https://discuss.elastic.co/t/how-to-build-my-elasticsearch-query/135142 "2018-06-08T11:37:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ExtraBall77](https://avatars.discourse-cdn.com/v4/letter/e/e19b73/32.png) [@ExtraBall77](https://discuss.elastic.co/u/ExtraBall77)\
**Post date:** [June 8, 2018, 11:37am UTC](https://discuss.elastic.co/t/how-to-build-my-elasticsearch-query/135142/1 "2018-06-08T11:37:12Z")

</div>

Hello,

I have a problem that seems simple, but I can not solve it. If in my index I have as data :

```
 country | id | n1 | n2 | n3 | other
---------+----+----+----+----+-------
       1 | 1 | 0 | 2 | 0 | 1
       2 | 2 | 1 | 0 | 1 | 1
       2 | 2 | 1 | 0 | 1 | 2
       2 | 2 | 1 | 0 | 1 | 3
       2 | 3 | 1 | 1 | 1 | 1
       1 | 4 | 0 | 3 | 2 | 1
       1 | 4 | 0 | 3 | 2 | 2

```

How do I run the following query in Elasticsearch?

```
 select DISTINCT country, id, n1, n2, n3 from MyIndex;
 country | id | n1 | n2 | n3
---------+----+----+----+----
       2 | 3 | 1 | 1 | 1
       1 | 4 | 0 | 3 | 2
       1 | 1 | 0 | 2 | 0
       2 | 2 | 1 | 0 | 1

```

And especially :

```
select country, sum(n1), sum(n2), sum(n3) from (select DISTINCT country, id, n1, n2, n3 from MyIndex) as foo group by country;
 country | sum | sum | sum
---------+-----+-----+-----
       1 | 0 | 5 | 2
       2 | 2 | 1 | 2
```

---

<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, 2018, 11:37am UTC](https://discuss.elastic.co/t/how-to-build-my-elasticsearch-query/135142/2 "2018-07-06T11:37:17Z")

</div>

Closed on July 6, 2018, 11:37am UTC

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