I am trying to run a sql query from python that establishes a connection to TeraData. In the query, I want to pass a column from a dataframe under the WHERE clause (this column has some IDs, against which I am trying to find the record).
I can successfully query the result if I hard code the column values inside the WHERE clause. But I do not want this as the values can be over 1000.
I would like to pass this column into the WHERE clause.
This is my code which is working file if I hardcode the values in the WHERE clause
# Import libraries
import pyodbc
import pandas as pd
# Set variables
teradata_driver = 'Teradata Database ODBC Driver 16.20'
teradata_host_name = 'XXXX-xxx-xxx.xx.xxx.xxx'
teradata_user = 'XXXXXX'
teradata_password = 'xxxxxx'
# enter the connection type
teradata_authentication = 'ODBC'
query= """
select * from <Table_Name> as table1
where table1.ProductID IN ('15800','15801','15802','15803','15804','15805','15806','15807')
and table1.ProductDate = date '2023-04-30'
and table1.prod_TYPE = 'LE'
"""
cnxn = pyodbc.connect("DRIVER={%s};DBCNAME=%s;UID=%s;PWD=%s;authentication=%s"\
%(teradata_driver,teradata_host_name,teradata_user,teradata_password,teradata_authentication))
df_teradata_result = pd.read_sql(query,cnxn)
I have a DataFrame which has a column called product_id (product id can be more than 1000s). How can I pass product_id inside the WHERE clause above. Thanks in advance.
I have tried the option provided here but it is not working for me Passing a Dataframe column in where clause of a SQL (Column is of type string)