I am working in a refactoring of my code. And I am trying to get the result of a python script in a single query.
I tried to do this:
select year(created), month(created), count(*) as total_id
from repository_clinicaltrial
group by id, year(created), month(created)
order by id, year(created), month(created);
to get the count of ids of each MONTH-YEAR.
Using python i am doing this way:
dic_months = {1: "jan",
2: "fev",
3: "mar",
4: "abr",
5: "mai",
6: "jun",
7: "jul",
8: "ago",
9: "set",
10: "out",
11: "nov",
12: "dez"
}
def retorna_dic_report(yearI):
yearN = yearI + 1
query_report = 'select created from repository_clinicaltrial where created >= "%s-01-01" and created < "%s-01-01";' % (yearI,
yearN)
db = MySQLdb.connect(host= host,
user= user,
passwd= pass,
db= db)
cur = db.cursor()
cur.execute(query_report)
dic_result = {}
for row in cur.fetchall():
try:
dic_result[dic_months[row[0].month]] = dic_result[dic_months[row[0].month]] + 1
except:
if row[0].month == 12:
dic_result[dic_months[row[0].month]] = 1
return dic_result
I want to use a single query that return the count of ids of each months in a range of years.
is it possible?