title: Python : Pass date list to PostgreSQL query and loop it n number of times
body: <p>Hi I am using PostgreSQL 12 and have an INSERT sql statement made up of few CTEs as below:</p>
  <pre><code>WITH CTE1 as (
  select date, count(*) from customers where date = %(date_list)s group by date
  ),
  CTE2 as (
  select date, count(*) from customers where account_deleted is NULL and date = %(date_list)s group by date
  )
  INSERT INTO customers_final (date, customers)
  select * from CTE1 
  union all
  select * from CTE2 
  </code></pre>
  <p>I now want to use Python to loop through a list of dates for different customer datasets (different snapshots of customers over time) and run the query above multiple times, one run for every customer dataset that corresponds to a different date on my list.</p>
  <pre><code>Example of list: ('2021-03-21', '2021-03-22', '2021-03-23', etc). 
  </code></pre>
  <p>The expected result should be one row (one CTE) per date per dataset. As we have 2 CTEs in the example we should be expecting 2 rows per date per dataset.</p>
  <p>The below should work for a single dataset. Not sure how to also loop through different tables (customer tables):</p>
  <pre><code>def insert_data(date_list: list):
  
  sql_query = &quot;&quot;&quot;WITH CTE1 as (
  select date, count(*) from customers where date = %(date_list)s group by date
  ),
  CTE2 as (
  select date, count(*) from customers where account_deleted is NULL and date = %(date_list)s group by date
  )
  INSERT INTO customers_final (date, customers)
  select * from CTE1 
  union all
  select * from CTE2&quot;&quot;&quot; (
  
   for date in date_list:
          if date_list is not None and len(date_list) &gt; 0:
              params = {&quot;date&quot;: date}
              cursor.execute(sql_query, {&quot;date&quot;: date})
  
          else:
              log.warning(f'No records found.')
  
  
  if __name__ == '__main__':
      date_list = ['2021-03-29', '2021-03-28', '2021-03-27', '2021-03-26']
      insert_data(date_list)
  </code></pre>