def create_table_as(sql, table_name, schema=None, override=False):
    """Reformats the query into the create table as query.

    Works only for the single select SQL statements, in all other cases
    the sql query is not modified.
    :param superset_query: string, sql query that will be executed
    :param table_name: string, will contain the results of the query execution
    :param override, boolean, table table_name will be dropped if true
    :return: string, create table as query
    """
    if schema:
        table_name = schema + '.' + table_name
    exec_sql = ''
    if override:
        exec_sql = 'DROP TABLE IF EXISTS {table_name};\n'
    exec_sql += 'CREATE TABLE {table_name} AS \n{sql}'
    return exec_sql.format(**locals())
