0

First of all, I appreciate your help with my problem. I am currently working in a data base using sql in python. This is my code:

from tabulate import tabulate #the output generates a table
sql_query = "SELECT DISTINCT dc.name AS disease_code_name, d.drug_name FROM disease_code dc, drug d, drug_disease dd WHERE dc.code_id=dd.code_id AND dd.drug_id=d.drug_id AND dd.inferred_score=(SELECT MAX(dd.inferred_score));"
cursor.execute(sql_query)
score_table = [['1','2']]
for row in cursor:
    nom = row[0]
    sco = row[1]
    list_d = [nom,sco]
    score_table.append(list_d)
print(tabulate(score_table, headers='firstrow', tablefmt='fancy_grid'))

The output that it generates is something similar to this:

1       2
apple   fruit
orange  fruit
lemon   fruit
pig     animal
lion    animal
tiger   animal

But I want something like this:

1      2
fruit  apple, lemon, orange
animal pig, lion, tiger

Can you help me please? Thank you very much for your help

quik1399
  • 175
  • 8

1 Answers1

0

Just using GROUP BY you can solve the problem. Thank you to Wander Nauta for his help

quik1399
  • 175
  • 8