Given an expensive query that aggregates temporal data,
    Revise the results to censor before a particular date

def revise_sql(query, id_column, output_table, max_date_column,
               min_date_column, date_column, date, source_id_column=None):
    """
    Given an expensive query that aggregates temporal data,
    Revise the results to censor before a particular date
    """
    if source_id_column is None:
        source_id_column = id_column

    if hasattr(id_column, '__iter__'):
        id_column = str.join(', ', id_column)
    if hasattr(source_id_column, '__iter__'):
        source_id_column = str.join(', ', source_id_column)

    sql_vars = dict(query=query, id_column=id_column, output_table=output_table,
                    max_date_column=max_date_column, min_date_column=min_date_column,
                    date_column=date_column, date=date, source_id_column=source_id_column)

    sql_vars['ids_query'] = """
    SELECT {id_column} FROM {output_table}
    WHERE {max_date_column} >= '{date}' AND {min_date_column} < '{date}'""" .format(**sql_vars)

    sql_vars['revised_query'] = query.replace(
            '1=1',
            "(({source_id_column}) in (select * from ids_query) and {date_column} < '{date}')"
            .format(**sql_vars))

    new_query = """
    with ids_query as ({ids_query})
    select * from ({revised_query}) t
    """.format(**sql_vars)

    return new_query