Skip to content

Instantly share code, notes, and snippets.

@merchantmehul
Created June 27, 2024 04:36
Show Gist options
  • Save merchantmehul/092d82060100b08d2295ddc6d83e570b to your computer and use it in GitHub Desktop.
Save merchantmehul/092d82060100b08d2295ddc6d83e570b to your computer and use it in GitHub Desktop.
get_cte function and code for more getting the sql
def get_cte(csv_path, table_name):
"""
Generates a Common Table Expression (CTE) SQL query from a CSV file.
Args:
csv_path (str): The file path to the CSV file.
table_name (str): The desired name for the CTE table.
Returns:
str: A CTE SQL query string that can be used to create a temporary table
with the data from the CSV file.
"""
df_csv = pd.read_csv(csv_path)
# Determining the Column Names
column_names = ', '.join(df_csv.columns.to_list())
sql_values = []
for index, row in df_csv.iterrows():
values_str = "(" + ", ".join([str(val) if isinstance(val, (int, float)) else f"'{val}'" for val in row.values]) + ")"
sql_values.append(values_str)
values_statement = ",\n".join(sql_values)
cte_query = f"""
WITH {table_name} ({column_names}) AS (
VALUES
{values_statement}
)
SELECT * FROM temp_product tp
"""
return cte_query
sql_file = open(output_sql, "w")
sql_file.write(get_cte(product_csv, 'temp_product'))
sql_file.close()
print(f"Please find the SQL script at the path {output_sql}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment