import pymysql conn = pymysql.connect(host='10.100.80.100', user='facturation', password='VD67owoj', database='gestionclient', cursorclass=pymysql.cursors.DictCursor) with conn.cursor() as cur: cur.execute(""" SELECT p.category, COUNT(*) as cnt, GROUP_CONCAT(DISTINCT p.sku) as skus FROM service s JOIN product p ON p.id = s.product_id WHERE s.status = 1 AND p.category NOT IN (4,9,17,21,32,33) GROUP BY p.category ORDER BY cnt DESC """) total = 0 for r in cur.fetchall(): print("cat={}: {} services — SKUs: {}".format(r["category"], r["cnt"], r["skus"])) total += r["cnt"] print("\nTotal missing active services: {}".format(total)) cur.execute(""" SELECT p.sku, COUNT(*) as cnt, p.price FROM service s JOIN product p ON p.id = s.product_id WHERE s.status = 1 AND p.category = 26 GROUP BY p.sku, p.price ORDER BY cnt DESC """) print("\nCategory 26 breakdown:") for r in cur.fetchall(): print(" {} x{} @ {:.2f}".format(r["sku"], r["cnt"], float(r["price"]))) conn.close()