# Using essql query results a second query against another index

**URL:** <https://discuss.elastic.co/t/using-essql-query-results-a-second-query-against-another-index/327396>\
**Category:** Kibana\
**Tags:** elastic-stack-sql, canvas\
**Created:** [March 9, 2023, 9:00pm UTC](https://discuss.elastic.co/t/using-essql-query-results-a-second-query-against-another-index/327396 "2023-03-09T21:00:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dreynolds](https://avatars.discourse-cdn.com/v4/letter/d/9dc877/32.png) [@dreynolds](https://discuss.elastic.co/u/dreynolds)\
**Post date:** [March 9, 2023, 9:00pm UTC](https://discuss.elastic.co/t/using-essql-query-results-a-second-query-against-another-index/327396/1 "2023-03-09T21:00:52Z")

</div>

In Canvas I'm attempting to query an index, return the results of a specific column, and then query another index with the results of the original query. An example would be two indices:  
Index 1: login\_activity; two fields: user.name, host.hostname  
Index 2: machine\_activity; 4 fields: host.hostname, host.ip, field3, field4

I'm attempting to determine which hosts USER1 has logged into, then pull all documents from "machine\_activity" that have a matching host.hostname.

Elasticstack 8.6.2

```auto
 kibana
| essql query="SELECT host.hostname FROM \"login_activity\" WHERE user.name='USER1'"
| joinRows column=host.hostname
| var_set name=hostnames
| essql query= "SELECT host.hostname FROM \"machine_activity\" WHERE host.hostname IN {var name=hostnames}"
| table
| render

```

I also tried

```auto
// | essql query= "SELECT host.hostname FROM \"machine_activity\" WHERE host.hostname IN \{var name=hostnames\}"
And
// | essql query= "SELECT host.hostname FROM \"machine_activity\" WHERE host.hostname IN ?" parameter={var name=hostnames}

```

I'm using joinRows and var\_set because essql accepts filters, not datatables, and I'm hoping that passing the string as a variable will allow me to bypass that limitation.

---

<div class="post-metadata">

**Author:** ![nickpeihl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickpeihl/32/112622_2.png) [@nickpeihl](https://discuss.elastic.co/u/nickpeihl)\
**Post date:** [March 10, 2023, 4:14pm UTC](https://discuss.elastic.co/t/using-essql-query-results-a-second-query-against-another-index/327396/2 "2023-03-10T16:14:38Z")

</div>

Hi Dan. I think one way to pass in the variable is to use the [string function](https://www.elastic.co/guide/en/kibana/current/canvas-function-reference.html#string_fn) to create the SQL string.

Maybe something like this?

```auto
| essql query={string "SELECT host.hostname FROM \"machine_activity\" WHERE host.hostname IN (" { var hostnames } ")" }

```

---

<div class="post-metadata">

**Author:** ![dreynolds](https://avatars.discourse-cdn.com/v4/letter/d/9dc877/32.png) [@dreynolds](https://discuss.elastic.co/u/dreynolds)\
**Post date:** [March 10, 2023, 6:42pm UTC](https://discuss.elastic.co/t/using-essql-query-results-a-second-query-against-another-index/327396/3 "2023-03-10T18:42:26Z")

</div>

Thank you Nick, that works perfectly.

---

<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 7, 2023, 6:42pm UTC](https://discuss.elastic.co/t/using-essql-query-results-a-second-query-against-another-index/327396/4 "2023-04-07T18:42:29Z")

</div>

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