I have two tables. Table 1
has all the unique places (30 rows) that I am interested in:
places |
---|
japan |
china |
india |
... |
Table 2
has all the information for IDs, places visited and date.
id | places | date |
---|---|---|
10001 | japan | 20210204 |
10001 | australia | 20210204 |
10001 | china | 20210204 |
10001 | argentina | 20210205 |
10002 | spain | 20210204 |
10002 | india | 20210204 |
10002 | china | 20210205 |
10003 | argentina | 20210204 |
10003 | portugal | 20210204 |
What I am interested to get is:
- For a specific date (let's say 20210204)
- Find all unique
IDs
from Table 2 that has visited at least one of theplaces
from Table 1 - Save those unique
IDs
to a temp table.
Here is what I have tried:
create temporary table imp.unique_ids_tmp
as select distinct(final.id) from
(select t2.id
from table2 as t2
where t2.date = '20210204'
and t2.places in
(select * from table1)) final;
I am struggling to incorporate the "at least one" logic such that once a satisfying id
is found, it stops looking at those id
records.