0

I have a bunch of repeating queries in a Python accessed SQL notebook. I've tested the below

colum_to_agg = f'''data_col_1'''

sql_aggregates_query=f'''
avg({colum_to_agg}) as (concat({colum_to_agg},'_mean')),
max({colum_to_agg}) as (concat({colum_to_agg},'_max')),
'''

print(sql_aggregates_query)

Which returns

avg(data_col_1) as (concat(data_col_1,'_mean')),
max(data_col_1) as (concat(data_col_1,'_max')),

Where i would like

avg(data_col_1) as data_col_1_mean,
max(data_col_1) as data_col_1_max,

I can get the query to run with

colum_to_agg = f'''data_col_1'''

sql_aggregates_query=f'''
avg({colum_to_agg}) as {colum_to_agg}{'_mean'},
max({colum_to_agg}) as {colum_to_agg}{'_max'},
'''
print(sql_aggregates_query)

But am interested to know whether using concat in an alias is possible

mapping dom
  • 1,737
  • 4
  • 27
  • 50

1 Answers1

1

Query can be written without the concat function.

colum_to_agg = f'''data_col_1'''

sql_aggregates_query=f'''
avg({colum_to_agg}) as {colum_to_agg}_mean,
max({colum_to_agg}) as {colum_to_agg}_max'''

print(sql_aggregates_query)

This should resolve the issue with query format.

nithish08
  • 468
  • 2
  • 7