How to use for loop to run sql query in python

Question:

I have 100 records in table A, I only list 4 here but please use loop to solve this question

A = ['A001', 'B001','C001','Bdd','djd.djsx']
AB = pd.DataFrame(A, columns = ['app_name']) 
AB

app_name
0   A001
1   B001
2   C001
3   Bdd
4   djd.djsx

I have SQL query looks like this

select COUNT (DISTINCT [R_ID]) 
from database_view
where [app_name] in ('A001')

I want to use loop to execute the query for 100 times and append the final result to the AB

What I tried is:

For i in AB:
    sql = '''select COUNT (DISTINCT [R_ID]) 
from database_view
where  [app_name] in {i}'''

    a=pd.read_sql_query(sql,database)
    final = AB.append(a,ignore_index = True)
final

but it does not work, and also I find it will add one more column to the right of df"AB", but it will append the count number to a new row at last.

Expected Result looks likes this,
expected result

it means for app A001, it has 6 different R_ID,
for app B001 it has 1 different R_ID

Any help is really appreicated!

Asked By: xlxdxf

||

Answers:

You forgot to put column name in for loop and f-string in SQL query. Try this code:

for i in AB['app_name']:
    sql = f'''select COUNT (DISTINCT [R_ID]) 
from database_view
where  [app_name] in {i}'''

Update-1: Edit SQL query:

# Create an empty dataframe:
df=pd.DataFrame()

for i in AB['app_name']:
    sql = f'''select app_name,COUNT (DISTINCT [R_ID]) 
from database_view
where  [app_name] in {i}
GROUP BY app_name'''
    a=pd.read_sql_query(sql,database)
    final = df.append(a,ignore_index = True)

print(final)
Answered By: Himanshu Panwar

Consider a single aggregate query using GROUP BY without any for-loop and the need to append to data frame. Once SQL query runs, filter result with DataFrame.query to your list of 100 apps.

sql = '''SELECT [app_name], COUNT(DISTINCT [R_ID]) AS r_id_count
         FROM database_view 
         GROUP BY [app_name]
      '''

apps = AB["app_name"].tolist()

app_counts_df = (
    pd.read_sql_query(sql, database)
      .query("app_name == @apps")
)
Answered By: Parfait
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.