# Is it possible to write it into single ES query ? (SQL: self join)

**URL:** https://discuss.elastic.co/t/is-it-possible-to-write-it-into-single-es-query-sql-self-join/22881
**Category:** Elasticsearch
**Created:** [March 25, 2015, 7:30am UTC](https://discuss.elastic.co/t/is-it-possible-to-write-it-into-single-es-query-sql-self-join/22881 "2015-03-25T07:30:22Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Vijay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vijay/32/816_2.png) [@Vijay](https://discuss.elastic.co/u/Vijay)
#### Post date: [March 25, 2015, 7:30am UTC](https://discuss.elastic.co/t/is-it-possible-to-write-it-into-single-es-query-sql-self-join/22881/1 "2015-03-25T07:30:22Z")

</div>

I have problem, I want to retrieve all the document wherever same user  
participate in event1 and event2.

{ "user": "user1", "event": "event1" }  
{ "user": "user2", "event": "event1" }  
{ "user": "user3", "event": "event2" }  
{ "user": "user1", "event": "event2" }  
{ "user": "user4", "event": "event1" }  
{ "user": "user1", "event": "event3" }

Is it possible to write it into single query ?

SQL query, something similar like this:  
select \* from userTable u1, u2 where u1.user = u2.user AND ( ( u1.event =  
event1 and u2.event = event2) OR ( u1.event = event2 and u2.event = event1))

Result should be:  
{ "user": "user1", "event": "event1" }  
{ "user": "user1", "event": "event2" }

Thanks,  
Vijay

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/d20f58a4-dfb4-4226-8b4b-9dae3d5bc00d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d20f58a4-dfb4-4226-8b4b-9dae3d5bc00d%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:24am UTC](https://discuss.elastic.co/t/is-it-possible-to-write-it-into-single-es-query-sql-self-join/22881/2 "2017-07-06T00:24:12Z")

</div>


