Suppose I have the following table in redshift:
table
| a | b |
|----:|----:|
| 3 | 1 |
| 1 | 8 |
| 7 | 6 |
| 4 | 0 |
| 5 | 6 |
| 5 | 2 |
| 5 | 9 |
| 4 | 3 |
| 7 | 9 |
| 9 | 8 |
And in python, I have the following list of tuples:
x = [(3,1), (4,2), (10, 1), (7,9), (5,2), (6,1)]
I want to extract all rows from the table where the tuple (a,b)
is in x using pd.read_sql_query`.
If I only had one column it would be a simple SQL WHERE clause, something like:
query = f'''
SELECT *
FROM table
WHERE a IN {x_sql}
'''
pd.read_sql_query(query, engine)
My final result would be:
| a | b |
|----:|----:|
| 3 | 1 |
| 5 | 2 |
| 7 | 9 |
I wanted to create a query like:
#doesn't work
SELECT *
FROM table
WHERE a,b IN ((3,1), (4,2), (10, 1), (7,9), (5,2), (6,1))