def filter_imei_list_sql_by_device_type(conn, exempted_device_types, imei_list_sql):
    'Function to return SQL filtering out exempted device types.'
    return sql.SQL('SELECT imei_norm\n                        FROM (SELECT imei_norm,\n                                     SUBSTRING(imei_norm, 1, 8) AS tac\n                                FROM ({0}) imeis) imeis_with_tac\n                        JOIN gsma_data\n                       USING (tac)\n                       WHERE device_type NOT IN {1}\n                    ').format(sql.SQL(imei_list_sql), sql.Literal(tuple(exempted_device_types))).as_string(conn)
Function to return SQL filtering out exempted device types.
def filter_imei_list_sql_by_device_type(conn, exempted_device_types, imei_list_sql):
    
    return sql.SQL('SELECT imei_norm\n                        FROM (SELECT imei_norm,\n                                     SUBSTRING(imei_norm, 1, 8) AS tac\n                                FROM ({0}) imeis) imeis_with_tac\n                        JOIN gsma_data\n                       USING (tac)\n                       WHERE device_type NOT IN {1}\n                    ').format(sql.SQL(imei_list_sql), sql.Literal(tuple(exempted_device_types))).as_string(conn)