# How convert this SQL query to Elasticsearch query?

**URL:** <https://discuss.elastic.co/t/how-convert-this-sql-query-to-elasticsearch-query/191133>\
**Category:** Elasticsearch\
**Created:** [July 18, 2019, 7:13am UTC](https://discuss.elastic.co/t/how-convert-this-sql-query-to-elasticsearch-query/191133 "2019-07-18T07:13:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![psyapathy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/psyapathy/32/50265_2.png) [@psyapathy](https://discuss.elastic.co/u/psyapathy)\
**Post date:** [July 18, 2019, 7:13am UTC](https://discuss.elastic.co/t/how-convert-this-sql-query-to-elasticsearch-query/191133/1 "2019-07-18T07:13:56Z")

</div>

Hello!

I have this SQL query:

```auto
SELECT time,targetuser, count(targetuser),ipaddress FROM `test_index` WHERE `event_id`=4444 AND `status`=`0x10` GROUP BY ipaddress,targetuser ORDER BY TIME DESC;

```

How convert this SQL query to Elasticsearch query? is it possible?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 18, 2019, 10:08am UTC](https://discuss.elastic.co/t/how-convert-this-sql-query-to-elasticsearch-query/191133/2 "2019-07-18T10:08:09Z")

</div>

You probably need to do a `terms` agg on `ipaddress` and then another inner one on `targetuser` and a `bool` query with 2 `term` clauses (one on `event_id` with a value of `4444`, and another one on `status` with a value of `"0x10"`) that you would put inside the `must` array of the `bool` query.

---

<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 15, 2019, 10:08am UTC](https://discuss.elastic.co/t/how-convert-this-sql-query-to-elasticsearch-query/191133/3 "2019-08-15T10:08:15Z")

</div>

Closed on August 15, 2019, 10:08am UTC

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