Home > Blockchain >  How to iteratively create UNION ALL SQL statement using Python?
How to iteratively create UNION ALL SQL statement using Python?

Time:05-24

I am connecting to Snowflake to query row count data of view table from Snowflake. I am also querying metadata related to View table. My Query looks like below. I was wondering if I can iterate through UNION ALL statement using python ? When I try to run my below query I received an error that says "view_table_3" does not exist.

Thanks in advance for your time and efforts!

Query to get row count for Snowflake view table (with metadata)

view_tables=['view_table1','view_table2','view_table3','view_table4']

print(f""" SELECT * FROM (SELECT TABLE_SCHEMA,TABLE_NAME,CREATED,LAST_ALTERED FROM SCHEMA='INFORMATION_SCHEMA.VIEWS' WHERE TABLE_SCHEMA='MY_SCHEMA' AND TABLE_NAME IN ({','.join("'"  x  "'" for x in view_tables)})) t1

LEFT JOIN
(SELECT 'view_table1' table_name2, count(*) as view_row_count from MY_DB.SCHEMA.view_table1
UNION ALL SELECT {','.join("'"  x  "'" for x in view_tables[1:])},count(*) as view_row_count from MY_DB.SCHEMA.{','.join(""  x  "" for x.replace("'"," ") in view_tables)})t2
on t1.TABLE_NAME =t2.table_name2 """)

CodePudding user response:

If you want to make a union dynamically, put the entire SELECT query inside the generator, and then join them with ' UNION '.

sql = f'''SELECT * FROM INFORMATION_SCHEMA.VIEWS AS v
LEFT JOIN (
    {' UNION '.join(f"SELECT '{table}' AS table_name2, COUNT(*) AS view_row_count FROM MY_SCHEMA.{table}" for table in view_tables)}
) AS t2 ON v.TABLE_NAME = t2.table_name2
WHERE v.TABLE_NAME IN ({','.join(f"'{table}'" for table in view_tables)})
'''
print(sql);
  • Related