You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To fix it in my case I added aliases to all columns:
defadd_alias_to_sql_select_fields(sql_query):
# Splitting the query into parts before and after the FROM clauseselect_part, from_part=sql_query.split("FROM")
# Splitting the SELECT part into fieldsfields=select_part.replace("SELECT", "").strip().split(",")
new_fields= []
forfieldinfields:
field=field.strip()
# Check if field already has an aliasif" as "notinfield.lower():
# Extracting the field name after the last dot for aliasfield_name=field.split(".")[-1].strip()
# Adding aliasfield_with_alias=f"{field} as {field_name}"else:
field_with_alias=fieldnew_fields.append(field_with_alias)
# Reconstructing the SELECT part with aliasesnew_select_part="SELECT\n "+",\n ".join(new_fields)
# Reconstructing the full querynew_sql_query=f"{new_select_part}\nFROM{from_part}"returnnew_sql_query# Your SQL querysql_query="""SELECT dj.field_1, cardinality(dj.field_1) as field_1_count, dj.field_2, cardinality(dj.field_2) as field_2_count, dj.field_3FROM dj"""# Adding aliasesnew_sql_query=add_alias_to_sql_select_fields(sql_query)
print(new_sql_query)
Output
SELECTdj.field_1as field_1,
cardinality(dj.field_1) as field_1_count,
dj.field_2as field_2,
cardinality(dj.field_2) as field_2_count,
dj.field_3as field_3
FROM dj
Query
Code
Output
Feature needed
The text was updated successfully, but these errors were encountered: