I'm trying to get top 10 prescriptions prescribed from the database into a dataframe using pd.read_sql(sql, uri), but it returned with the following error:
~\AppData\Local\Continuum\anaconda3\envs\GISProjects\lib\site-packages\sqlalchemy\engine\result.py in _non_result(self, default)
1168 if self._metadata is None:
1169 raise exc.ResourceClosedError(
-> 1170 "This result object does not return rows. "
1171 "It has been closed automatically."
1172 )
ResourceClosedError: This result object does not return rows. It has been closed automatically.
My query has local variables to track ranking so that it'll return top 10 prescription by practice. It works if I run it in MySql Workbench but not when I use pd.read_sql()
sql = """
SET @current_practice = 0;
SET @practice_rank = 0;
select practice, bnf_code_9, total_items, practice_rank
FROM (select a.practice,
a.bnf_code_9,
a.total_items,
@practice_rank := IF(@current_practice = a.practice, @practice_rank + 1, 1) AS practice_rank,
@current_practice := a.practice
FROM (select rp.practice, rp.bnf_code_9, sum(rp.items) as total_items
from rx_prescribed rp
where ignore_flag = '0'
group by practice, bnf_code_9) a
order by a.practice, a.total_items desc) ranked
where practice_rank <= 10;
"""
df = pd.read_sql(sql, uri)
I expect it to return the data and into pandas dataframe but it returned with error. I assume it was from the first statement, which sets the local variable. The first two statements are necessary so that the data returns with top 10.
It works fine without the first two statements, however, it would return with '1' in all rows for the practice_rank column rather than expected values of 1, 2 ,3 and so on.
Is there a way I can run multiple statements and return the results from the last statement executed?